I have a JSON response that contains information about a user.
{
"userId": "123456789",
"email": "\"[email protected]",
"firstName": "\"foo\"",
"lastName": "\"bar\"",
"name": "\"foo bar",
"bio": "\"boo baz\"",
"age": "42"
}
I'd like to create 2 models, User and Profile from the same JSON, with a single request.
I'd then like Profile to be a property on the User struct.
At the moment, my standard struct looks like this -
struct User: Codable, Equatable {
var userId: String
var email: String
var firstName: String
var lastName: String
var name: String?
var bio: String?
var age: Int?
}
I would prefer however to do something like this -
struct User: Codable, Equatable {
var userId: String
var email: String
var profile: UserProfile // I'd like to wrap the profile info up in this field
}
struct UserProfile: Codable, Equatable {
var firstName: String
var lastName: String
var name: String?
var bio: String?
var age: Int?
}
As profile does not exist on the response, I do not understand how I can decode the rest of the response into it.