@objcMembers
public class MyResponse: NSObject, Codable {
public let id: String?
public let context: String?
public let results: [MyResult]?
}
What is the proper way to parse MyResponse from Data in class extension?
I tried the following, but got error "Cannot assign to value: 'self' is immutable. Cannot assign value of type 'MyResponse' to type 'Self'."
extension MyResponse {
public convenience init(data: Data) throws {
self = try JSONDecoder().decode(MyResponse.self, from: data)
}
}
Decodable
type a new initializer, you can create a dummy protocol to which only your type conforms to and then extend that dummy protocol. This is the solution used internally in the Swift standard library. For more details, see my answer to a related question. – Laufer