You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
structUser{letid:Intletname:Stringletemail:String?letrole:RoleletcompanyName:Stringletfriends:[User]}extensionUser:Decodable{staticfunc decode(j:JSON)->Decoded<User>{returncurry(User.init)<^> j <|"id"<*> j <|"name"<*> j <|?"email"// Use ? for parsing optional values<*> j <|"role"// Custom types that also conform to Decodable just work<*> j <|["company","name"]// Parse nested objects<*> j <||"friends"// parse arrays of objects}}// Wherever you receive JSON data:letjson:AnyObject?=try?NSJSONSerialization.JSONObjectWithData(data, options:[])
if let j:AnyObject= json {letuser:User?=decode(j)}
classVehicle:Model{letmake=Property<String>(key:"make")letmodel=Property<String>(key:"model", required: true)letyear=Property<Int>(key:"year"){ year in
if year <2015{// offer discount}}letcolor=Property<UIColor>(key:"color", defaultValue:UIColor.blackColor())}// instantiate objectletvehicle=Vehicle(json: json)// get property typeprintln("Vehicle make property has type: \(vehicle.make.type)")// get property value
if let make = vehicle.make.value {println("Vehicle make: \(make)")}
https://github.com/storehouse/JSON