小编All*_*n.L的帖子

如何使用ObjectMapper映射不同类型?

我正在使用ObjectMapper将 JSON 映射到 Swift 对象。

我有以下 Swift 对象:

class User: Mappable {

    var name: String?
    var val: Int?

    required init?(map: Map) { }

    func mapping(map: Map) {
        name <- map["name"]
        val  <- map["userId"]
    }
}
Run Code Online (Sandbox Code Playgroud)

我有这个 JSON 结构:

{
   "name": "first",
   "userId": "1" // here is `String` type.
},
{
   "name": "second",
   "userId": 1 // here is `Int` type.
}
Run Code Online (Sandbox Code Playgroud)

映射JSON后,其中的userId值为null 。Username"first"

我怎样才能映射Int/StringInt

json swift objectmapper

1
推荐指数
1
解决办法
4060
查看次数

标签 统计

json ×1

objectmapper ×1

swift ×1