This is how I declared imageurls
inside My Object
:
var imageURLs = List<String>()
and parsing:
func mapping(map: Map) {
imageURLs <- map["image_urls"]
}
and this is what I am trying to parse:
{ "image_urls": ["a"] }
At the end above property is empty. Why?
Using Realm 3.3 so array of primitives should work.
In case you are working with both Realm an ObjectMapper, there is a pretty cool option for you, by using ObjectMapper+Realm , you would be able to map arrays directly to realm lists, as follows:
func mapping(map: Map) {
imageURLs <- (map["image_urls"], ListTransform<String>())
}
Note that by default the object mapper is unable to map arrays as realm lists, which is possible by using the above library.
You can create a variable in the function and map items to an array:
func mapping(map: Map) {
var pathes = [String]()
pathes <- map["image_urls"]
self.imageUrls.add(pathes)
}
Or you could use the extension, which called ObjectMapper+Realm
, as written above.
You can map array of String using ObjectMapper like below, this works fine:
import ObjectMapper
import RealmSwift
import ObjectMapper_Realm
public class Items: Object, Mappable {
var images = List<ListImages>()
@objc dynamic var id = 0
required convenience public init?(map : Map){
self.init()
}
public override class func primaryKey() -> String? {
return "id"
}
public func mapping(map: Map) {
var images: [String]? = nil
images <- map["image"]
images?.forEach { image in
let value = ListImages()
value.value = image
self.images.append(value)
}
}
}
class ListImages:Object{
@objc dynamic var value: String = ""
}
I have solved the problem using RealmTypeCastTransform()
In your code just add this
func mapping(map: Map) {
imageURLs <- (map["image_urls"], RealmTypeCastTransform())
}
Now import ObjectMapperAdditions
in the beginning of your file
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.