Removing Dependencies – ObjectMapper

Introduction


Hello developers, this blog post and the nexts to follow are blog posts series in order to help you unlock you code by removing all the dependencies created when you install a cocoapod. The solution is quite simple, remove the cocoapods and start writing your own code! Have a good look and thought about it,  it’s time to become an engineer!

Good Old Networking


NSURLConnection and NSJSONSerialization, seriously what else do you need ? I learned them and I made my own Networking Models which they work fantastic! I really prefer writing my own code and especially for the Networking Component, in contrast with the majority of the developers out there who use the well-known Cocoapods, Alamofire and ObjectMapper extensively. So this post will try to encourage the reader to start writing his own code and leave Cocoapods aside.

The Open-Source Community


I am in favour of the open-source Cocoapods community but why not write your own code, you are the developer after all. Isn’t it better not seeing a podfile, instead of seeing “Analysing 42 dependencies…” on which you don’t know on what your application is dependent on. When I started learning Swift I was lucky enough my friend Mike taught me the basics of Networking without the use of Cocoapods. Learning from the ground up helped me a lot to understand how the networking part works. But there are developers out there with a few years of iOS experience and they don’t know how to make a simple API request and map objects without the use of Cocoapods.

The Revenge of the Swift


This year is the time to create your own Networking Model and you will carry on working on it, it will be yours, it will freaking work until iOS 56 and you wont have to make pull requests in case you wish to add something new.

Swift 4 is willing to help you by introducing two new protocols, Decodable & Encodable so you don’t even have to use JSONSerialization, but with 1 line of code, maps your server data into your native object. Before saying “bye bye” to ObjectMapper, let’s remember how you can map JSON objects using ObjectMapper.

Model:

struct Temperature: Mappable {
    var celsius: Double?
    var fahrenheit: Double?

    init?(map: Map) {}

    mutating func mapping(map: Map) {
        celsius <- map["celsius"]
        fahrenheit <- map["fahrenheit"]
    }
}

Mapping:

let user = Mapper<User>().map(JSONString: JSONString)

Before saying something about this library I would like to see the Swift 4 way and lets sum them later on.

Model:

struct Temperature: Codable {
    var celsius: Double?
    var fahrenheit: Double?
}

Note that the variable name must be exactly as the Server Response name or you could use  a CodingKey to decode with your custom keys.

Mapping:

let decoder = JSONDecoder()
let object = try! decoder.decode(Book.self, from: response)

For sure the swift 4 way is much more simple and clean. Good job 👍🏽, now you are ready to cross out this ObjectMapper pod of your podfile.

In the next blog post I will make the same example this time with URLSession instead of using Alamofire.