struct BlogPost {
let id: Int
let text: String
let thumbURL: NSURL
let linkURL: NSURL
}
extension BlogPost {
private struct Keys {
static let id = "id"
static let text = "text"
static let thumbURL = "thumb_url"
static let linkURL = "link_url"
}
init?(JSON: AnyObject) {
guard let id = JSON[Keys.id] as? Int,
let text = JSON[Keys.text] as? String,
let jsonThumbURL = JSON[Keys.thumbURL] as? String,
let jsonLinkURL = JSON[Keys.linkURL] as? String,
let thumbURL = NSURL(string: jsonThumbURL),
let linkURL = NSURL(string: jsonLinkURL) else {
return nil
}
self.id = id
self.text = text
self.thumbURL = thumbURL
self.linkURL = linkURL
}
}
http://ctarda.com/2016/06/this-is-how-i-parse-json-in-swift/