I've created a network manager that gets a JSON response and converts it in to a Swift object,
protocol HTTPResponse: Codable {
}
class UserResponse: HTTPResponse {
var name: String?
}
let responseObj = try? JSONDecoder().decode(T.self, from: response.data!)
where T
is an HTTPResponse decendent
I need to be able to create an 'empty' UserResponse object, an instance where all the class variables are nil
I changed HTTPResponse
from protocol
to class
and added an empty required init() {}
to it.
This allows me to invoke T()
and get an instance of UserResponse
but now JSONDecoder
is no longer parsing the JSON, it also creates instances where all variables are nil
How can I achieve the end result where JSONDecoder
works well, and I can create stub instances of my HTTPResponse
decedent classes?
I'm working with swift 4.2