-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContents.swift
38 lines (29 loc) · 1.11 KB
/
Contents.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Builder Patern
let jsonString = "{\"items\":[{\"givenName\":\"BJ\",\"familyName\":\"Miller\",\"heightInInches\":77},{\"givenName\":\"Cole\",\"familyName\":\"Miller\",\"heightInInches\":51}]}"
struct Person {
let givenName: String
let familyName: String
let heightInInches: Int
}
struct PersonBuilder : BuilderType {
typealias ItemType = Person
func people(json: String?, keyName key: String = "items") -> [Person] {
do {
let items = try objectsFromJSON(json, forKey: key, withParsingFunction: parsePerson)
return items
} catch {
return [] // ideally handle error vs return empty array
}
}
func parsePerson(items: JSONDictionary) -> Person? {
guard let givenName = items["givenName"] as? String, familyName = items["familyName"] as? String, heightInInches = items["heightInInches"] as? Int else { return nil }
let person = Person(givenName: givenName, familyName: familyName, heightInInches: heightInInches)
return person
}
}
let builder = PersonBuilder()
let people = builder.people(jsonString)
people.count
people.forEach { person in
print(person)
}