Skip to content

Instantly share code, notes, and snippets.

@yingmu52
Created August 21, 2017 16:29
Show Gist options
  • Save yingmu52/ba78a7001deddd1b3dffd70f953bda70 to your computer and use it in GitHub Desktop.
Save yingmu52/ba78a7001deddd1b3dffd70f953bda70 to your computer and use it in GitHub Desktop.
Demo Mapping json using ObjectMapper
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let urlstring = "http://jsonplaceholder.typicode.com/posts"
Alamofire.request(urlstring).responseJSON { (data) in
let results = Mapper<Post>().mapArray(JSONObject: data.result.value)
for post in results! {
print(post.id)
print(post.title)
print(post.content)
}
}
}
}
struct Post: Mappable {
var id: Int
var title: String
var content: String
var userId: Int
init?(map: Map){
id = 0
title = ""
content = ""
userId = 0
}
mutating func mapping(map: Map) {
id <- map["id"]
title <- map["title"]
content <- map["body"]
userId <- map["userId"]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment