Reputation: 863
I'm new with Decodable in swift. I just learned how to use it but I'm stuck with trying to decode a subclass within another class's init(from decoder: ).
The question is how do I decode this other class from the original class init?
I get a user JSON file looking like this
{
firstName:Tim,
LastName: Apple,
...
socialNetworks: [
{
name: Facebook,
username: Tim85,
...
},
{
name: Twitter,
username: Tim_85,
...
},
...
],
}
I have a User class that looks like this
class User: Codable {
firstName: String,
lastName: String,
...
socialNetworks: [SocialNetwork]
enum CodingKeys: String, CodingKey {
case firstName, lastName, ..., socialNetworks
}
required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.firstName = try container.decodeIfPresent(String.self, forKey: .firstName) ?? ""
self.lastName = try container.decodeIfPresent(String.self, forKey: .lastName) ?? ""
// How do I also decode SocialNetworks???
}
...
}
I also have a SocialNetwork class.
class SocialNetwork: Codable {
name: String,
username: String,
...
enum CodingKeys: String, CodingKey {
case name, username, ...
}
required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.name = try container.decodeIfPresent(String.self, forKey: .name) ?? ""
self.username = try container.decodeIfPresent(String.self, forKey: .userName) ?? ""
}
...
}
Upvotes: 1
Views: 524
Reputation: 100503
You don't have to write any custom init
struct Root: Codable {
let firstName, lastName: String
let socialNetworks: [SocialNetwork]
enum CodingKeys: String, CodingKey { // you can remove this block if LastName starts with l small instead of L
case firstName
case lastName = "LastName"
case socialNetworks
}
}
struct SocialNetwork: Codable {
let name, username: String
}
For a try it would be
self.socialNetworks = try container.decodeIfPresent([SocialNetwork].self, forKey: .socialNetworks) ?? []
But the decoder do this for you
Upvotes: 2