-
Notifications
You must be signed in to change notification settings - Fork 39
Home
John Lui edited this page Oct 13, 2017
·
7 revisions
class People: JSONNDModel {
@objc var name = ""
}
let json = JSONND(dictionary: ["name": "JohnLui"])
let people = People(JSONNDObject: json)
print(people.name) // get "JohnLui"
class Good : JSONNDModel {
@objc var des = ""
required init(JSONNDObject json: JSONND) {
super.init(JSONNDObject: json)
self.des = json["description"].stringValue
}
}
auto reflection from JSON to Model need every variable in JSONNDModel class has a default value:
class People: JSONNDModel {
@objc var name = ""
@objc var number = 0
@objc var array = [Int]()
}
multi-level reflection need a init in init() function:
class TestModel: JSONNDModel {
@objc var string = ""
@objc var double = 0.0
@objc var int = 0
@objc var array_values = [Int]()
@objc var array = [ModelOnlyOneKey]()
@objc var hey: Hey!
required init(JSONNDObject json: JSONND) {
super.init(JSONNDObject: json)
for i in json["array_values"].arrayValue {
self.array_values.append(i.intValue)
}
for i in json["array"].arrayValue {
self.array.append(ModelOnlyOneKey(JSONNDObject: i))
}
self.hey = Hey(JSONNDObject: json["hey"])
}
}
class ModelOnlyOneKey: JSONNDModel {
@objc var key = 0
}
class Hey: JSONNDModel {
@objc var man: Man!
required init(JSONNDObject json: JSONND) {
super.init(JSONNDObject: json)
self.man = Man(JSONNDObject: json["man"])
}
}
class Man: JSONNDModel {
@objc var hello = false
}