Alamofire.swift(三)-URLRequest

作者: Mr_Legend | 来源:发表于2016-11-08 22:29 被阅读192次
    extension URLRequest {
        public init(url: URLConvertible, method: HTTPMethod, headers: HTTPHeaders? = nil) throws {
            let url = try url.asURL()
    
            self.init(url: url)
    
            httpMethod = method.rawValue
    
            if let headers = headers {
                for (headerField, headerValue) in headers {
                    setValue(headerValue, forHTTPHeaderField: headerField)
                }
            }
        }
    
        func adapt(using adapter: RequestAdapter?) throws -> URLRequest {
            guard let adapter = adapter else { return self }
            return try adapter.adapt(self)
        }
    }
    

    url是继承了URLConvertible协议的类型

    method是HTTPMethod枚举类型

    public enum HTTPMethod: String {
    case get = "GET"
    case head = "HEAD"
    case post = "POST"
    ...
    }

    headersHTTPHeaders类型,其实就是通过类型重命名typealias关键字,将key-value都为String类型的字典重命名为HTTPHeaders

    //A dictionary of headers to apply to a URLRequest.
    public typealias HTTPHeaders = [String: String]

    并且可以看出,url和method都是必须要有的,headers是可选的


    let url = try url.asURL()

    这里用try,是因为asURL()定义在类型转换时出现错误throws异常

    self.init(url: url)

    这里调用URLRequest的初始化方法
    //Creates and initializes a URLRequest with the given URL and cache policy.
    public init ( url: URL, cachePolicy: URLRequest.CachePolicy = default, timeoutInterval: TimeInterval = default )
    这里的CachePolicy其实是NSURLRequest,默认是:.useProtocolCachePolicy
    public typealias CachePolicy = NSURLRequest.CachePolicy


    httpMethod = method.rawValue

    举个官方的🌰,貌似挺好理解

    enum PaperSize: String {
        case A4, A5, Letter, Legal
    }
    let selectedSize = PaperSize.Letter
    print(selectedSize.rawValue)
    // Prints "Letter"
    print(selectedSize == PaperSize(rawValue: selectedSize.rawValue)!)
    // Prints "true"
    

    if let headers = headers {
                for (headerField, headerValue) in headers {
                    setValue(headerValue, forHTTPHeaderField: headerField)
                }
            }
    

    for循环的这种方式是由于headers是元组类型


    当看到using adapter: RequestAdapter?的时候,我已经知道这是个协议了,而且有throws,说明我们在return的时候需要try。
    使用guard来判断是否有值,比if更加清晰

    public protocol RequestAdapter {
        func adapt(_ urlRequest: URLRequest) throws -> URLRequest
    }
    

    相关文章

      网友评论

        本文标题:Alamofire.swift(三)-URLRequest

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