2016-04-07 37 views

回答

1

Alamofire建立在NSURLSession之上,並且與(POST/GET/PUT/etc)進行REST交互的代碼行少。它會讓你「90%」的方式,但如果你需要做超級專業的網絡調用,你將需要使用NSURLSession。

例如:簡單Alamofire呼叫得到JSON

Alamofire.request(.GET, "https://www.google.com/",encoding: .JSON).responseJSON {response in 
     if(response.result.error == nil){ 
      print(response.data) 
     }else{ 
      print("Network Error") 
     } 
} 

NSURLConnection的現在已經過時,並NSURLSession是新的標準。 https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/URLLoadingSystem/Articles/UsingNSURLSession.html

0

對於雨燕3.0及以上,Alamofire是最好的,因爲它是很好的優化,也可重複使用的,它有也features.Alamofire許多構建呼籲在一個類似的方法由符合協議創建了一個路由器, URLRequestConvertible。在引擎蓋下,Alamofire呼籲建立在NSURLSessionConfiguration之上的單例模式。

+0

現在swift4.0使用Codable進行了新的json解析。所以在這裏,Alamofire變得過時和漫長。相反,吊艙'CodableAlamofire'就是替代品。 您可以查看下面提到的網址: https://github.com/Otbivnoe/CodableAlamofire –

相關問題