Skip to content

Instantly share code, notes, and snippets.

@NSExceptional
Created January 20, 2016 21:57
Show Gist options
  • Save NSExceptional/0384fb9801411f2eb536 to your computer and use it in GitHub Desktop.
Save NSExceptional/0384fb9801411f2eb536 to your computer and use it in GitHub Desktop.
An example of how to use Mantle in Swift, assuming you have a bridging header that #imports Mantle.h
class MYModel: MTLModel, MTLJSONSerializing {
@objc private(set) var name: String?
@objc private(set) var identifier: String?
@objc private(set) var count: Int = 0
class func JSONKeyPathsByPropertyKey() -> [NSObject : AnyObject]! {
return ["name": "model.name",
"identifier": "model.id",
"count": "model.c"]
}
}
let dict = ["model": ["name": "ThePantsThief", "id": "abcde12345", "count": 21]]
let model = try? MTLJSONAdapter.modelOfClass(MYModel.self, fromJSONDictionary: dict)
let name = model?.name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment