【发布时间】:2015-08-07 13:10:26
【问题描述】:
我正在研究如何从类对象列表中构造或创建子对象。
我有一个 Category 类,看起来像:
class Category {
var Code: Int?
var Label: String?
init(Code: Int, Label: String) {
self.Code = Code
self.Label = Int
}
}
然后我有一个类别列表var categories = [Category]()
然后我像这样附加我的列表:
categories.append(5,"Shoes")
如何构造一个如下所示的 json 对象:
{
"List":[
{
"Code":"5",
"Label":"Shoes"
},
....
]
}
【问题讨论】:
-
考虑使用像 ObjectMapper 这样的外部框架,例如 github.com/Hearst-DD/ObjectMapper
-
我会推荐使用 swiftyJSON。 github.com/SwiftyJSON/SwiftyJSON