美文网首页
swift 之 Alamofire5.5

swift 之 Alamofire5.5

作者: 风依旧_c080 | 来源:发表于2022-06-28 15:48 被阅读0次

Alamofire是由原AFNetworking团队研发的适用swift的网络请求框架。

struct ResponseType: Codable {
        let success: String
        let message: String
        let data: [String]
    }
    
    public class func request(url:String,
                              modelType:HTTPMethod = .post,
                              parameters:[String : String] = [:],
                              successBlock:@escaping (_ responseData:[String : Any]) -> Void){
        
        let header:HTTPHeaders = ["Accept-Language":"zh-TW","token":""]

// JSONParameterEncoder.default -- 相当于AFN里面的[AFJSONRequestSerializer serializer]
// URLEncodedFormParameterEncoder.default-- 相当于AFN里面的AFPropertyListRequestSerializer
        AF.request(url, method: modelType, parameters: parameters, encoder: URLEncodedFormParameterEncoder.default, headers: header, interceptor: nil, requestModifier: nil).responseDecodable(of: ResponseType.self) { response in
                        
            if let data = response.data {
                
                let dict = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers)
                print("-*-*-*-*-*-:",String(data: data, encoding: .utf8)!)
                successBlock(dict as! Dictionary)
            }
            
        }
        
    }

相关文章

网友评论

      本文标题:swift 之 Alamofire5.5

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