加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift ——~——~第三方网络请求封装

发布时间:2020-12-14 06:35:37 所属栏目:百科 来源:网络整理
导读:最近闲了下来,做个demo看了下swift3.0相比2.0发生了多大的变化 费话不多说,直接上代码 static func getRequest(url:NSString,dic:NSDictionary,cla:AnyClass,progress:((Progress)-Void)?,success:@escaping ((URLSessionDataTask,Any?)-Void),failure:((UR
最近闲了下来,做个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 } }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读