Array of strings is not saved to Realm object using ObjectMapper

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.


Solution 1:

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.

Solution 2:

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.