Swift - ObjectMapper:映射 jsonString
我有两个模型:
class CellModel: StaticMappable {
static func objectForMapping(map: Map) -> BaseMappable? {
return nil
}
func mapping(map: Map) {
id <- map["id"]
title <- map["title"]
description <- map["description"]
}
private var id: Int
private var title: String
private var description: String
init(id: Int, title: String, description: String) {
self.id = id
self.title = title
self.description = description
}
}
和
class CellModelArray: StaticMappable {
var cells = [CellModel]()
static func objectForMapping(map: Map) -> BaseMappable? {
return nil
}
func mapping(map: Map) {
cells <- map["cells"]
}
}
我从这样的对象创建了一个 jsonString:
let jsonString = Mapper().toJSONString(rootModel, prettyPrint: false)
json 看起来像这样:
{"cells":[{"id":0,"title":"Header","description":"Description"},{"description":"Description","id":0,"title":"Header"},{"description":"Description","id":1,"title":"Header"},{"id":0,"title":"Header","description":"Description"},{"description":"Description","title":"Header","id":0}]}
然后我想获取这个字符串并将其转换回对象,但是当我这样尝试时:
var cells = CellModelArray()
cells = Mapper<CellModelArray>().map(JSONString: code) ?? CellModelArray()
它不起作用并返回零。感谢您的帮助。
I have two models:
class CellModel: StaticMappable {
static func objectForMapping(map: Map) -> BaseMappable? {
return nil
}
func mapping(map: Map) {
id <- map["id"]
title <- map["title"]
description <- map["description"]
}
private var id: Int
private var title: String
private var description: String
init(id: Int, title: String, description: String) {
self.id = id
self.title = title
self.description = description
}
}
and
class CellModelArray: StaticMappable {
var cells = [CellModel]()
static func objectForMapping(map: Map) -> BaseMappable? {
return nil
}
func mapping(map: Map) {
cells <- map["cells"]
}
}
I created a jsonString from object like this:
let jsonString = Mapper().toJSONString(rootModel, prettyPrint: false)
and json looks like this:
{"cells":[{"id":0,"title":"Header","description":"Description"},{"description":"Description","id":0,"title":"Header"},{"description":"Description","id":1,"title":"Header"},{"id":0,"title":"Header","description":"Description"},{"description":"Description","title":"Header","id":0}]}
Then I want to take this string and convert it back to the object, but when I try it like that:
var cells = CellModelArray()
cells = Mapper<CellModelArray>().map(JSONString: code) ?? CellModelArray()
it does not work and returns nil. Thank you for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请删除
ObjectMapper
。它是一个优秀的库,但自 Swift 4(2017 年推出)以来,它已经过时,取而代之的是内置的、更快速
Codable
协议。主要好处是模型文件可以是结构(值类型),并且更加可靠,因为开发人员不必关心文字字符串键
这就足够了
可以使用此代码对给定的 JSON 字符串
进行解码和编码
Please drop
ObjectMapper
.It's a library of merit but since Swift 4 (introduced 2017) it has become obsolete in favor of the built-in and swiftier
Codable
protocol.The main benefit is that the model files can be structs (value types) and it's much more reliable because the developer doesn't have to take care of literal string keys
This is sufficient
The given JSON string
can be decoded and encoded with this code