Objectmapper get array of one item within JSON

后端 未结 1 455
-上瘾入骨i
-上瘾入骨i 2021-01-23 15:39

So I have the following JSON, which I am using together with ObjectMapper and Realm.

{
  \"result\": [
    {
      \"id\": 20,
      \"types\": [
        \"now\"         


        
1条回答
  •  再見小時候
    2021-01-23 16:00

    Because Realm cannot detect assigning List properties since List property is not Objective-C type. So List properties should be declared as let, and should not be nil. You should use append/remove.../insert...method to modifying theList`.

    So your code

    typez <- map["types"]
    

    doesn't work, since you assign values to the typez property directly.

    The workaround is like the following:

    func mapping(map: Map) {
        ...
        var typez: [String]? = nil
        typez <- map["types"]
    
        typez?.forEach { t in
            let obj = getType()
            obj.text = t
            self.typez.append(obj)
        }
        ...
    

    First, store the mapped value to the local variable (it is string array). Then convert the string array to objects. Then append the objects to the List property.

    0 讨论(0)
提交回复
热议问题