A: 假設(shè)一切都運(yùn)行良好!現(xiàn)在開(kāi)始下一步!
B:FUCK * 10086
國(guó)慶七天就我們兩個(gè)基佬值班了,所以準(zhǔn)備利用這七天時(shí)間把IOS熟悉一下,這樣在寫(xiě)API的時(shí)候就可以自己直接在自己的APP里面測(cè)試了。
我的教材是《Swift IOS 應(yīng)用開(kāi)發(fā)實(shí)戰(zhàn)》(多看上花了25RMB買(mǎi)的,說(shuō)是實(shí)戰(zhàn)其實(shí)也沒(méi)有太多內(nèi)容,而且現(xiàn)在更新了swift2.0,很多東西都不一樣,不過(guò)還是值得一看),總之一口氣看到第七章的時(shí)候,決定停下來(lái)做一個(gè)app練練手。于是就有了下面這個(gè)頁(yè)面:
首先聲明,這是萬(wàn)里長(zhǎng)城的第一步而已
然后,這些數(shù)據(jù)來(lái)自于php API:
于是,開(kāi)始在ios內(nèi)獲取并解析json數(shù)據(jù):
let urlString = "http://101.200.74.199/todo/v1"
let session = NSURLSession.sharedSession()
let todoUrl = NSURL(string: urlString)
let task = session.dataTaskWithURL(todoUrl!){
(data, response, error) -> Void in
if error != nil{
print(error?.localizedDescription)
}else{
do{
let jsonData = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSDictionary
let code = jsonData?["code"] as? Int
if code == 200{
let data = jsonData?["data"] as? NSArray
dispatch_sync(dispatch_get_main_queue(), { () -> Void in
for thing in data!{
let thing = Thing(data: thing as! NSDictionary)
self.things.append(thing)
self.tableView.reloadData()
}
})
}else{
print(jsonData?["message"] as? String)
}
}catch{
print("在處理JSON時(shí)拋出了異常")
self.tabBarItem.title = "網(wǎng)絡(luò)繁忙"
}
}
}
task.resume()

