Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Kyle Melton
6,664 PointsCan you init a struct from another struct's init?
I'm getting a large array of person information which contains address information. I want o generate a struct of person, with an optional array of addresses (also a struct). For some reason I cannot get it to do it.
https://gist.github.com/kalm42/18452dc1a94cc6de7ffde046bd0dc243
Here is a sample of the code I have.
1 Answer
Kyle Melton
6,664 PointsThe problem was my struct was not instantiated when I attempted to append the new item to the array.
The fix was
self.addresses = []
for address in json["address"] as! [[String:String]] {
let newAddress = Address(dictionary: address)
newAddress
self.addresses?.append(newAddress)
}