I have a codable model
public final class MyCodableModel: Codable {
public let id: Int
}
I also have another model that happens to have the same variables inside it.
public final class MyOtherModel {
public let id: Int
}
Now, I want to instantiate MyCodableModel
using MyOtherModel
in an extension. I don't want to modify MyCodableModel
directly because of dependency issues.
I first tried to use a normal initializer in my extension, but it said I needed to use a convenience initializer
, so then I ended up with this:
extension MyCodableModel {
convenience init?(myOtherModel: MyOtherModel) {
id = myOtherModel.id
}
}
But the error says 'let' property 'id' may not be initialized directly; use "self.init(...)" or "self = ..." instead
. I assume this is because I'm not using the designated initializer of init(from: Decoder)
.
Is there another way to do this? Or will I not be able to convert MyOtherModel
to a MyCodableModel
?