Alamofire4.x开源代码分析(一)使用方法

Alamofire4.x开源代码分析(一)使用方法,第1张

概述本着了解框架的实现思路和学习Swift的目的开启本系列的博客.本系列参考Alamofire官方文档和自己的一些理解,欢迎指正和指教. 配置要求 iOS 8.0+, macOS 10.10.0+, tvOS 9.0+ and watchOS 2.0+ Xcode 8.1+ Swift 3.0+ 简单使用 Alamofire.request("https://httpbin.org/get").res

本着了解框架的实现思路和学习Swift的目的开启本系列的博客.本系列参考Alamofire官方文档和自己的一些理解,欢迎指正和指教.

配置要求 iOS 8.0+,macOS 10.10.0+,tvOS 9.0+ and watchOS 2.0+ Xcode 8.1+ Swift 3.0+ 简单使用
Alamofire.request("https://httpbin.org/get").responseJsON { response in            print("Request: \(String(describing: response.request))")   // // 原始的请求            print("Response: \(String(describing: response.response))") // http 请求响应            print("Result: \(response.result)")                         // 响应结果标识                        if let Json = response.result.value {                print("JsON: \(Json)") // JsON序列化的数据            }                        if let data = response.data,let utf8Text = String(data: data,enCoding: .utf8) {                print("Data: \(utf8Text)") // 经过UTF-8编码的数据            }        }

不难看出alamofire用了链式编程的思想.

响应数据类型

请求返回五种response

// Response Handler -直接返回URL session delegate的数据         func response(         queue: dispatchQueue?,completionHandler: @escaPing (DefaultDataResponse) -> VoID)         -> Self                  // Response Data Handler - 序列化为Data         func responseData(         queue: dispatchQueue?,completionHandler: @escaPing (DataResponse<Data>) -> VoID)         -> Self                  // Response String Handler - 序列化成String字符串         func responseString(         queue: dispatchQueue?,enCoding: String.EnCoding?,completionHandler: @escaPing (DataResponse<String>) -> VoID)         -> Self                  // Response JsON Handler - 序列化成JsON         func responseJsON(         queue: dispatchQueue?,completionHandler: @escaPing (DataResponse<Any>) -> VoID)         -> Self                  // Response PropertyList (pList) Handler - 序列化成pList         func responsePropertyList(         queue: dispatchQueue?,completionHandler: @escaPing (DataResponse<Any>) -> VoID))         -> Self

被序列化的数据可以用通过resonse中的result.value来获取数据

//也可以同时使用,内部通过线程队列实现        Alamofire.request("https://httpbin.org/get")            .responseString { response in                print("Response String: \(response.result.value)")            }            .responseJsON { response in                print("Response JsON: \(response.result.value)")        }

Response是默认在主线程队列执行异步回调,

(queue ?? dispatchQueue.main).async

这里的队列也是可以自定义的,例如:

let utilityQueue = dispatchQueue.global(qos: .utility)Alamofire.request("https://httpbin.org/get").responseJsON(queue: utilityQueue) { response in    print("Executing response handler on utility queue")}
响应验证

valIDate可以在结果返回之前验证响应状态或类型是否正确,也可以使用默认方式验证.valIDate()

Alamofire.request("https://httpbin.org/get")    .valIDate(statusCode: 200..<300)    .valIDate(ContentType: ["application/Json"])    .responseData { response in        switch response.result {        case .success:            print("ValIDation Successful")        case .failure(let error):            print(error)        }    }
Response缓存

Response缓存基于系统的URLCache,如果需要自定义参数在后面 Session Manager Configurations章节中会讲到

系列目录

Alamofire4.x开源代码分析(一)使用方法 Alamofire4.x开源代码分析(二)请求参数和编码 Alamofire4.x开源代码分析(三)下载数据(更新中) 总结

以上是内存溢出为你收集整理的Alamofire4.x开源代码分析(一)使用方法全部内容,希望文章能够帮你解决Alamofire4.x开源代码分析(一)使用方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1061485.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存