Skip to content

Instantly share code, notes, and snippets.

@kalm42
Created April 22, 2017 18:10
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 kalm42/18452dc1a94cc6de7ffde046bd0dc243 to your computer and use it in GitHub Desktop.
Save kalm42/18452dc1a94cc6de7ffde046bd0dc243 to your computer and use it in GitHub Desktop.
Swift error with nested init
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