【问题标题】:How to map in Realm List an array of integers from JSON with ObjectMapper如何使用 ObjectMapper 在领域列表中映射来自 JSON 的整数数组
【发布时间】:2018-11-02 10:33:37
【问题描述】:

我有 JSON:

{ 
"id": 5,
"name": "Name of object",
"filter_ids": [ 10, 15, 20 ] 
}

我使用ObjectMapper 将 JSON 解析为领域对象。

我在对象中有变量:

dynamic var identifier: Int = 0 
dynamic var name: String = "" 
var filter_ids = List<Int>() 

如何映射filter_ids var?

func mapping(map: Map)  {
     identifier <- map["id"]
     name <- map["name"]
     filter_ids <- map["filter_ids"] //?????? not work
}

【问题讨论】:

  • 与您的问题无关,但不需要使用 3rd 方框架来解析 JSON,您可以简单地使用 Swift 4 的Decodable 协议。

标签: swift realm objectmapper realm-list


【解决方案1】:

我用过 cocoaPod(carthage 也可以)ObjectMapperAdditions

filter_ids <- (map["filter_ids"], RealmTypeCastTransform())

它适用于 Int、String、Bool 数组。

Duplicated discussion

【讨论】:

    猜你喜欢
    • 2015-10-25
    • 1970-01-01
    • 2015-11-27
    • 2017-04-26
    • 2018-03-28
    • 2016-02-02
    • 2016-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多