美文网首页
Swift 5 Alamofire框架网络工具类封装

Swift 5 Alamofire框架网络工具类封装

作者: Johnson_9d92 | 来源:发表于2022-01-08 22:50 被阅读0次

    Swift 5 Alamofire框架网络工具类封装

    import UIKit
    import Alamofire
    import SwiftyJSON
    
    
    class CLLNetWork: NSObject {
       static func getNetWork(vc:UIViewController, url:String, type:HTTPMethod, params:[String:Any]?,success:@escaping(_ response :[String: JSON])->(),failure:@escaping(_ response :String)->() ){
           CLLHUD.showAlway(view: vc.view)
           Alamofire.AF.request(url, method: HTTPMethod.get, parameters: params).responseJSON { (dataResponse) in
               CLLHUD.hide(view: vc.view)
               switch dataResponse.result {
               case .success:
                   let json = dataResponse.data?.toJSON().dictionaryValue
                   if json!["code"]?.intValue == 0{
                       success(json!)
                   }else{
                       CLLHUD.makeToast(string: json!["msg"]!.stringValue)
                       failure(json!["msg"]!.stringValue)
                   }
                   
               case let .failure(error):
                   CLLHUD.makeToast(string: error.localizedDescription)
                   failure(error.localizedDescription)
                   print(error)
               }
           }
       }
       
    
    }
    
    extension Data {
       func toJSON()-> JSON {
           return JSON(self)
       }
    }
    

    相关文章

      网友评论

          本文标题:Swift 5 Alamofire框架网络工具类封装

          本文链接:https://www.haomeiwen.com/subject/pimycrtx.html