Created
April 22, 2017 18:10
-
-
Save kalm42/18452dc1a94cc6de7ffde046bd0dc243 to your computer and use it in GitHub Desktop.
Swift error with nested init
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 Address { | |
let locationName: String? | |
let line1: String | |
let line2: String? | |
let line3: String? | |
let city: String | |
let state: String | |
let zip: String | |
init(dictionary: [String:String]) { | |
self.locationName = dictionary["locationName"] | |
self.line1 = dictionary["line1"]! | |
self.line2 = dictionary["line2"] | |
self.line3 = dictionary["line3"] | |
self.city = dictionary["city"]! | |
self.state = dictionary["state"]! | |
self.zip = dictionary["zip"]! | |
} | |
} | |
struct Person { | |
let name: String | |
var addresses: [Address]? | |
init(json: [String: Any]) { | |
self.name = json["name"] as! String | |
for address in json["address"] as! [[String:String]] { | |
self.addresses?.append(Address(dictionary: address)) | |
} | |
} | |
} | |
let json: [String:Any] = ["name":"Kyle Melton","address":[["line1": "The White House", "line2": "1600 Pennsylvania Avenue NW", "city": "Washington", "state": "DC", "zip": "20500"], ["line1": "331 Hart Senate Office Building", "city": "Washington", "state": "DC", "zip": "20510"],["line1": "California State Capitol", "line2": "P.O. Box 942849", "line3": "Room 5164", "city": "Sacramento", "state": "CA", "zip": "94249-0060"]]] | |
let me = Person(json: json) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment