在View ?Controller中
/** 模型數(shù)據(jù)源 */
? ? vardataSource : [Model] = [Model]()
? ? /** 控件拖線屬性 */
? ? @IBOutletweakvarmainTableView:UITableView!
? ? overridefuncviewDidLoad() {
? ? ? ? super.viewDidLoad()
? ? ? ? mainTableView.rowHeight = 100
? ? ? ? let urlString = "http://api.jisuapi.com/news/get"
? ? ? ? letpar : [String:Any] = [
? ? ? ? ? ? "":"",
? ? ? ? ? ? "":0,
? ? ? ? ? ? "":10,
? ? ? ? ? ? "" : ""
? ? ? ? ? ? ]
?NetworkTools.sharedInstance.request(.GET, urlString: urlString, parameters: par) { (result, error)in
? ? ? ? ? ? guarderror ==nilelse{
? ? ? ? ? ? ? ? return
? ? ? ? ? ? }
? ? ? ? ? ? guardletjsonDict = resultelse{
? ? ? ? ? ? ? ? return
? ? ? ? ? ? }
? ? ? ? ? ? letdict = jsonDictas!NSDictionary
? ? ? ? ? ? letresultDict = dict.value(forKey:"result")as!NSDictionary
? ? ? ? ? ? letlistArray = resultDict.value(forKey:"list")as!NSArray
? ? ? ? ? ? foritemin? listArray{
? ? ? ? ? ? ? ? letmodel =Model(dict: itemas! [String:Any])
? ? ? ? ? ? ? ? self.dataSource.append(model)
? ? ? ? ? ? }
? ? ? ? ? ? self.mainTableView.reloadData()
? ? ? ? }
? ? }
// MARK:- 協(xié)議
extension ViewController : UITableViewDataSource {
? ? functableView(_tableView:UITableView, numberOfRowsInSection section:Int) ->Int{
? ? ? ? return dataSource.count
? ? }
? ? functableView(_tableView:UITableView, cellForRowAt indexPath:IndexPath) ->UITableViewCell{
? ? ? ? letcell = tableView.dequeueReusableCell(withIdentifier:"cell")
? ? ? ? cell?.textLabel?.text=dataSource[indexPath.row].title
? ? ? ? cell?.detailTextLabel?.text=dataSource[indexPath.row].content
? ? ? ? cell?.detailTextLabel?.numberOfLines = 2
? ? ? ? returncell!
? ? }
/** 請(qǐng)求類(lèi)型枚舉 */
enumRequestType :String{
? ? caseGET ="GET"
? ? casePOST ="POST"
}
// MARK:- 創(chuàng)建網(wǎng)絡(luò)請(qǐng)求工具類(lèi)的單例對(duì)象
class NetworkTools: AFHTTPSessionManager {
? ? /** let本身就時(shí)線程安全的.在多個(gè)線程中能保證只創(chuàng)建一次 */
? ? staticletsharedInstance :PDNetworkTools= {
? ? ? ? letinstence =PDNetworkTools()
? ? ? ? /** 設(shè)置數(shù)據(jù)內(nèi)容樣式(在集合內(nèi)插入新的數(shù)據(jù)格式) */
? ? ? ? instence.responseSerializer.acceptableContentTypes?.insert("text/html")
? ? ? ? instence.responseSerializer.acceptableContentTypes?.insert("text/plain")
? ? ? ? returninstence
? ? }()
}
// MARK:- 封裝GET和POST請(qǐng)求
extension PDNetworkTools {
? ? /** 參數(shù)名稱(chēng)
?? ? 參數(shù)progress./下載進(jìn)度
?? ? 參數(shù)task./任務(wù)對(duì)象
?? ? 參數(shù)request./請(qǐng)求得到數(shù)據(jù)
?? ? 參數(shù)error./如果下載失敗的錯(cuò)誤信息 */
? ? /** 封裝的請(qǐng)求方法,外界調(diào)用通過(guò)閉包得到結(jié)果 */
? ? funcrequest(_requestType :RequestType, urlString :String, parameters : [String:Any], finished :@escaping(Any?,Error?) -> ()) {
? ? ? ? /** 下載進(jìn)度progressCallBack */
? ? ? ? letprogressCallBack : ((Progress) ->Void)? = { (progress)in
? ? ? ? ? ? /** 下載進(jìn)度 */
? ? ? ? }
? ? ? ? /** 成功successCallBack */
? ? ? ? letsuccessCallBack : ((URLSessionDataTask,Any?) ->Void)? = { (task, request)? in
? ? ? ? ? ? /** 下載成功 */
? ? ? ? ? ? finished(request,nil)
? ? ? ? }
? ? ? ? /** 失敗failureCallBack */
? ? ? ? let? failureCallBack : ((URLSessionDataTask?,Error) ->Void)? = { (task, error)in
? ? ? ? ? ? /** 請(qǐng)求失敗 */
? ? ? ? ? ? finished(nil, error)
? ? ? ? }
? ? ? ? /** 判斷請(qǐng)求類(lèi)型 */
? ? ? ? ifrequestType == .GET{
? ? ? ? ? ? /** 發(fā)送GET請(qǐng)求 */
? ? ? ? ? ? get(urlString, parameters: parameters, progress: progressCallBack, success: successCallBack, failure: failureCallBack)
? ? ? ? }else{
? ? ? ? ? ? /** 發(fā)送POST請(qǐng)求 */
? ? ? ? ? ? post(urlString, parameters: parameters, progress: progressCallBack, success: successCallBack, failure: failureCallBack)
? ? ? ? }
? ? }
}