This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typealias JSON = AnyObject | |
protocol JSONParsable { | |
init(json: JSON) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Profile:JSONParsable { | |
init(json: JSON) { | |
if let id:Int = json["id"] as? Int { | |
identifier = id | |
} | |
if let n:String = json["name"] as? String { | |
name = n | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Profile { | |
var identifier = 0 | |
var name = "" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
infix operator <-- {} | |
func <-- <T>(inout left: T, right: AnyObject?) { | |
if let v: T = right as? T { | |
left = v | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Profile:JSONParsable { | |
init(json: JSON) { | |
identifier <-- json["id"] | |
name <-- json["name"] | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func <-- <T>(inout left: T?, right: AnyObject?) { | |
if let v: T = right as? T { | |
left = v | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
infix operator <== {} | |
func <== <T:JSONParsable>(inout left:T, right: AnyObject?) { | |
if let r: AnyObject = right { | |
left = T.self(json:r) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Stats:JSONParsable { | |
init(json: JSON) { | |
numberOfFollowers <-- json["numberOfFollowers"] | |
numberOfFollowing <-- json["numberOfFollowing"] | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Profile:JSONParsable { | |
init(json: JSON) { | |
identifier <-- json["id"] | |
name <-- json["name"] | |
stats <== json["stats"] | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"id": 15678, | |
"name": "John Doe", | |
"stats": { | |
"numberOfFollowers": 163, | |
"numberOfFollowing": 10987 | |
} | |
} |