Get Updates By E-mail 

Article Written By:
Johann Dowa

Open Source Library For Easily Mapping JSON Data To/From Swift Objects

  •  
  •  
  •  
  •  
  •  

Earlier this year I mentioned the SwiftyJSON and JSONHelper libraries for working with JSON data in Swift.

Here’s another Swift based library for working with JSON data called ObjectMapper from Hearst-DD allowing you to easily map objects to and from JSON.

ObjectMapper allows you to easily map data to Swift objects by implementing their protocol with support for url and date transformations, custom transformations, and nested objects in your JSON.

This example from the readme shows an example mapping with the date transformed using the DateTransform function:

class User: MapperProtocol {

    var username: String?
    var age: Int?
    var weight: Double?
    var arr: [AnyObject]?
    var dict: [String : AnyObject] = [:]
    var friend: User?                       // Nested User object
    var birthday: NSDate?

    required init(){}

    // MapperProtocol    
    func map(mapper: Mapper) {
        username < = mapper["username"]
        age <= mapper["age"]
        weight <= mapper["weight"]
        arr <= mapper["arr"]
        dict <= mapper["dict"]
        friend <= mapper["friend"]
        birthday <= (mapper["birthday"], DateTransform())
    }
}

Once the class is set up you can then map your JSON to/from the object using a single line of code.

You can find ObjectMapper on Github here.

A nice Swift library for working with JSON data.


  •  
  •  
  •  
  •  
  •  

Submit A Resource

Have you created a useful tutorial, library or tool for iOS development that you would like to get in front of our 300,000+ monthly page views from iOS developers?

You can submit the url here.

The resources we feel will appeal to our readers the most will be posted on the front page.

Comments