ios - Swift: Parsing Array of object -
i have contact
object can have , array of address
objects.
class contact { var firstname: string = "" var lastname: string = "" var middlename: string = "" var id: int = -1 var addresses: array<address> = []
how initialize each address
object while fetching json
dictionary?
init(json: dictionary<string, anyobject>) { if let line1 = json["streetline"] as? string { self.streetline1 = line1 } if let city = json["city"] as? string { self.city = city } if let state = json["state"] as? string { self.state = state } if let zip = json["zip"] as? int { self.zip = zip }
tried doing this:
if let addressmap = json["addresses"] as? array<dictionary<string, anyobject?>> { address as? address in addressmap { addresses.append(address) } }
try this:
if let dictionaryarray = json["addresses"] as? array<dictionary<string, anyobject?>> address in dictionaryarray { var address = address(json: address) addresses.append(address) } }
so list of dictionarys json, loop through them , use init function inject data class.
Comments
Post a Comment