對于swift的網(wǎng)絡(luò)請求用的是第三方的框架Alamofire,簡單看了下算是可以使用了,但是還有很多不理解的地方,這里介紹使用方法,為了使自己方便理解,如果錯誤敬請指正??!
Alamofire.request("https://httpbin.org/get", method: HTTPMethod.get, parameters: nil, encoding: URLEncoding.default, headers: nil).responseJSON { (response) in
print("responseJSON:--\(response)")
if let error = response.result.error{
//如果有錯誤信息就打印錯誤,沒有就解析數(shù)據(jù)
print(error)
}else if let jsonresult = response.result.value{
let JSONDictionary = JSON(jsonresult)
//用SwiftJSON解析數(shù)據(jù)
let data = JSONDictionary["headers"].dictionary
let accept = data?["Accept"]
print("accept--\(accept!)")//加上!強(qiáng)制解析使其變?yōu)榉强蛇x類型
}
}
對于GET方法我沒有使用參數(shù)所以parameters設(shè)置為nil,可以根據(jù)自己的需要進(jìn)行設(shè)置參數(shù)。
具體可以參考:
http://www.cnblogs.com/sunshine-anycall/p/5170372.html
http://www.cnblogs.com/taoxu/p/5462599.html
// ?? 這個符號,我怕有初學(xué)者忘記了的提醒一下,A ?? B 這是一個 NIL合并運(yùn)算符,它的作用是如果 A 不是NIL 就返回前面可選類型參數(shù) A 的確定值, 如果 A 是NIL 就返回后面 B 的值!A和B之間類型的注意點我就不說了,忘記了去看書,,哈哈哈
http://blog.csdn.net/perla_/article/details/52396284 關(guān)于swift上拉加載和下拉刷新的博客