Codementor Events

How I learned ObjectMapper

Published Apr 27, 2019
How I learned ObjectMapper

About me

I am an experienced iOs developer with a focus on Swift language and have a hand on Objective-C

Why I wanted to learn ObjectMapper

I had converted my old app codes from Objective-C to Swift, and that's why I needed an Object Mapper

How I approached learning ObjectMapper

I did some research and found some relevant pods, and this library was the best choice over others because it has a very reach documentation and a very good community support.

If you want to start using ObjectMapper pod, try following process:

  • Go to the Github repository
  • Find the documentation link and take a deep look on it
  • Then import the pod into your project

That's it... easy, right?

Challenges I faced

I had a problem during the implementation process. The main challenge I've had was it didn't support Arrays by default.
If you are facing the same problem, the solution to this is as follow:

  • Easily create an empty Array from the object you want to map its keys
  • Loop through your Array and add each item to the mapper list

See below example:

    let  coupons = List<Coupon>()
    
    required convenience public init?(map: Map) {
        self.init()
    }
    
    public func mapping(map:Map) {
        var cps: [Coupon]? = nil
        cps <- map ["coupon"]
        cps?.forEach({ (coupon) in
            self.coupons.append(coupon)
        })
    }
Discover and read more posts from Ana Hejazi
get started