課程筆記文集地址:Udemy課程:The Complete iOS 9 Developer Course - Build 18 Apps
這節(jié)課主要講了如何處理 JSON 數(shù)據(jù)。
JSON全稱: JavaScript Object Notation
特點(diǎn):比 XML 更易用,更簡(jiǎn)單。
這節(jié)課里的 JSON 來源網(wǎng)址是 http://www.telize.com/geoip
HTTP的安全性處理,之前課程已經(jīng)講過了,在 .plist 文件里添加即可。
關(guān)鍵方法:
NSJSONSerialization.JSONObjectWithData
這個(gè)方法是處理 JOSN 的方法。放到整個(gè)代碼里如下:
let url = NSURL(string: "http://www.telize.com/geoip")!
let task = NSURLSession.sharedSession().dataTaskWithURL(url) { (data, response, error) -> Void in
if let urlContent = data {
do {
// 這個(gè)是處理 JSON 的關(guān)鍵方法
let jsonResult = try NSJSONSerialization.JSONObjectWithData(urlContent, options: NSJSONReadingOptions.MutableContainers)
print(jsonResult)
print(jsonResult["city"])
} catch {
print("JSON serialization failed")
}
}
}
task.resume()