Created
August 21, 2017 16:29
-
-
Save yingmu52/ba78a7001deddd1b3dffd70f953bda70 to your computer and use it in GitHub Desktop.
Demo Mapping json using ObjectMapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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