Code snippets for the blog post "JSON and the arguments" at http://www.apokrupto.com/blog-1/2016/4/3/json-and-the-arguments
Last active
June 11, 2016 12:39
-
-
Save warren-gavin/c5393c3e85435b4fac451da020a4320a to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Hole { | |
let length: Int | |
let number: Int | |
let par: Int | |
} | |
struct GolfCourse { | |
let name: String | |
let holes: [Hole] | |
} |
This file contains hidden or 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
{ | |
"name": "My Course", | |
"holes": [ | |
{ | |
"number": 1, | |
"length": 143, | |
"par": 3 | |
}, | |
{ | |
"number": 2, | |
"length": 340, | |
"par": 4 | |
} | |
] | |
} |
This file contains hidden or 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 JSONFormat = [String: AnyObject] | |
extension Hole { | |
typealias Format = JSONFormat | |
init?(_ holeInfo: Format) { | |
guard let length = holeInfo["length"] as? Int, let number = holeInfo["number"] as? Int, let par = holeInfo["par"] as? Int else { | |
return nil | |
} | |
self.length = length | |
self.number = number | |
self.par = par | |
} | |
} | |
extension GolfCourse { | |
typealias Format = JSONFormat | |
init?(_ courseInfo: Format) { | |
guard let name = courseInfo["name"] as? String, | |
let holeInfo = courseInfo["holes"], | |
let holes = (holeInfo as? [Hole.Format])?.flatMap({ Hole($0) }) where holes.count == holeInfo.count else { | |
return nil | |
} | |
self.name = name | |
self.holes = holes | |
} | |
} |
This file contains hidden or 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 Hole { | |
typealias Format = [String: Int] | |
init?(_ holeInfo: Format) { | |
guard let length = holeInfo["length"], let number = holeInfo["number"], let par = holeInfo["par"] else { | |
return nil | |
} | |
self.length = length | |
self.number = number | |
self.par = par | |
} | |
} |
This file contains hidden or 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
protocol JSONType { | |
} | |
extension NSString: JSONType {} | |
extension NSNumber: JSONType {} | |
extension NSArray: JSONType {} | |
extension NSDictionary: JSONType {} | |
typealias JSONFormat= [String: JSONType] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment