Skip to content

Instantly share code, notes, and snippets.

@wata-gh
Created May 18, 2016 14:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wata-gh/3f4f01e767f580cfddfd5aa0629ed1dd to your computer and use it in GitHub Desktop.
Save wata-gh/3f4f01e767f580cfddfd5aa0629ed1dd to your computer and use it in GitHub Desktop.
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