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"
...
}
headers是HTTPHeaders
類型,其實就是通過類型重命名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
}