Created
May 18, 2016 14:55
-
-
Save wata-gh/3f4f01e767f580cfddfd5aa0629ed1dd to your computer and use it in GitHub Desktop.
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 Group: Decodable { | |
let name: String | |
let floor: Int | |
let locationName: String | |
let optional: [String]? | |
// MARK: Decodable | |
static func decode(e: Extractor) throws -> Group { | |
return try Group( | |
name: e <| "name", | |
floor: e <| "floor", | |
locationName: e <| [ "location", "name" ], // Parse nested objects | |
optional: e <||? "optional" // Parse optional arrays of values | |
) | |
} | |
} | |
func testGroup() { | |
var JSON: [String: AnyObject] = [ "name": "Himotoki", "floor": 12 ] | |
let g = try? Group.decodeValue(JSON) | |
XCTAssert(g != nil) | |
XCTAssert(g?.name == "Himotoki") | |
XCTAssert(g?.floor == 12) | |
XCTAssert(g?.optional == nil) | |
JSON["name"] = nil | |
do { | |
try Group.decodeValue(JSON) | |
} catch let DecodeError.MissingKeyPath(keyPath) { | |
XCTAssert(keyPath == "name") | |
} catch { | |
XCTFail() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment