前端之家收集整理的这篇文章主要介绍了
swift ——~——~第三方网络请求封装,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
最近闲了下来,做个demo看了下swift3.0相比2.0发生了多大的变化 费话不多说,直接上
代码 static func getRequest(url:NSString,dic:NSDictionary,cla:AnyClass,progress:((Progress)->Void)?,success:@escaping ((URLSessionDataTask,Any?)->Void),failure:((URLSessionDataTask?,Error)->Void)?){ let sesson = AFHTTPSessionManager.init(); sesson.requestSerializer.timeoutInterval = 15; sesson.responseSerializer.acceptableContentTypes = NSSet.init(objects: "application/json","text/json","text/javascript","text/html") as? Set<String>; sesson.get(url as String,parameters: dic,progress: { (Progress) in print(Progress); },success: { (URLSessionDataTask,dictionary) in let Dict = dictionary as! NSDictionary; print(Dict); let code = Dict["code"] as! NSInteger; if code == 1 { let dddsf = Dict["datas"] as! NSDictionary let aaa = dddsf.allValues[0] as! NSArray; let sdfsdsdf = NSMutableArray.init(array: aaa) success(URLSessionDataTask,HttpTowWork.model(array: sdfsdsdf as! NSMutableArray,cla:cla)); }else{ } },failure: { (URLSessionDataTask,Error) in print(Error); failure!(URLSessionDataTask,Error); }); } static func model(array:NSMutableArray,cla:AnyClass)->(NSArray){ let array = NSMutableArray.init(array:array) return NSMutableArray.init(array: RMMapper.mutableArray(of: cla,fromArrayOfDictionary: array)) } 外部
调用 func HotGroupBuying(){ HttpTowWork.getRequest(url: APIWorkClass.hotDeals(),dic: NSDictionary.init(),cla: ShopGoodsModel.classForKeyedUnarchiver(),progress: { (Progress) in },idClass) in let array = idClass as! NSArray print(array) self.dataArray.replaceObject(at: 0,with: array) self.collectionView .reloadData() }) { (URLSessionDataTask,Error) in } }
原文链接:https://www.f2er.com/swift/322573.html