Skip to content

Latest commit

 

History

History
104 lines (81 loc) · 2.82 KB

README.md

File metadata and controls

104 lines (81 loc) · 2.82 KB

magic-mapper-swift

🌟 Super light and easy automatic JSON to model mapper

  • Finish writing README.md
  • Ability to map NSManagedObject
  • Ability to convert model back to dictionary

How to setup

Step 1: Get the JSON model

{
  "total_count": 176552,
  "incomplete_results": false,
  "items": [
    {...}
  ]
}

Step 2: Write the Swift models

class GithubRepository: NSObject, Mappable {
    var id        : String = ""
    var name      : String = ""
    var body      : String = ""
    var url       : String = ""
    var photo     : String = ""
    var createdAt : Date?
}

class GithubSearch: NSObject, Mappable {
    var total : Int = 0
    var items : [GithubRepository] = []
}

Step 3: Link your custom namings with the JSON model

The mapFromDictionary property lets you customize the properties mappings. You can also access nested values within dictionaries and arrays (example: emails.0.address).

var mapFromDictionary: [String : String] {
    return [
        "body"      : "description",
        "photo"     : "owner.avatar_url",
        "createdAt" : "created_at"
    ]
}

Step 4: Custom properties

The mapFromDictionaryTypes property let's you customize the type of the JSON property (only for optionals). If you have other Swift structures that you need to use, just extend them using the Mappable protocol.

var mapFromDictionaryTypes: [String : Mappable.Type] {
    return [
        "createdAt" : Date.self
    ]
}

For instance createdAt is of type Date and the JSON property is of type String. In order to convert the String to a Date type, just extend it using the Mappable protocol and it will automatically know to convert and set the value to the model.

extension Date: Mappable {
    
    init?(from: Any) {
        if let value = from as? String {
            let formatter = DateFormatter()
            formatter.dateFormat = "YYYY-MM-dd'T'HH':'mm':'ss'Z'"
            if let date = formatter.date(from: value) {
                self.init(timeIntervalSince1970: date.timeIntervalSince1970)
                return
            }
        }
        
        self.init()
    }
}

And that's it!

You can now populate your model from the generic dictionary that comes from your network layer. Happy coding :)

Alamofire.request(APIURL).responseJSON { (response) in
    if let dictionary = response.result.value as? KeyValue {
        self.feed = GithubSearch(dictionary)
    }
}

Contributing

There's still work to do here! Our goal is to speed up development times, making JSON mapping easy as 1-2-3. We would love to see you involved in this project!