I am using ObjectMapper for most of my projects inside NetworkManager and I tried to use Codable after WWDC17 I even create public library to use it but without luck is because it creates new object every time. this is not the case all the time.
for example imaging we have two requests
first request will respond with this
{
"balance": 45.30
}
and second request will respond with this
{
"purchased": [
"iPhone",
"iPad",
"Macbook Pro"
]
}
and swift object
struct Wallet: Codable {
private(set) var balance: Double
var purchased = [String]()
func requestBalance() { ... }
func requestPurchased() { ... }
}
when use let wallet = try! decoder.decode(Wallet.self, for: jsonData)
will create new object.
Object mapper have this class function
let wallet = Mapper<Wallet>().map(JSONObject: jsonData, toObject: object)
which will map the coming JSON data to the existing object and also create new one with just the new data. This approach been very helpful in almost every situation.
I propose to overload function decode
with decode(Wallet.self, for: jsonData, to: Object)
to have the same functionality