協議和擴展
查看這段代碼先看到的是兩個protocol和實現這些協議的extension
23D7248B-5377-4FFC-9529-C128F37208C0.png
- URLConvertible:構造一個URL,實現的類有String,URL,URLComponents,方法具體實現都比較簡單。
public protocol URLConvertible {
/// Returns a URL that conforms to RFC 2396 or throws an `Error`.
///
/// - throws: An `Error` if the type cannot be converted to a `URL`.
///
/// - returns: A URL or throws an `Error`.
func asURL() throws -> URL
}
- URLRequestConvertible:構造一個URLRequest,實現的類只有URLRequest
/// Types adopting the `URLRequestConvertible` protocol can be used to construct URL requests.
public protocol URLRequestConvertible {
/// Returns a URL request or throws if an `Error` was encountered.
///
/// - throws: An `Error` if the underlying `URLRequest` is `nil`.
///
/// - returns: A URL request.
func asURLRequest() throws -> URLRequest
}
extension URLRequestConvertible {
/// The URL request.
public var urlRequest: URLRequest? { return try? asURLRequest() }
}
extension URLRequest: URLRequestConvertible {
/// Returns a URL request or throws if an `Error` was encountered.
public func asURLRequest() throws -> URLRequest { return self }
}
在Alamofire中需要使用URL和URLRequest的地方都用對應的Protocol表示,不直接使用對應類型,這個設計讓代碼顯得更加靈活。
extension對URLRequest添加了一個init和adapt方法。
extension URLRequest {
/// Creates an instance with the specified `method`, `urlString` and `headers`.
///
/// - parameter url: The URL.
/// - parameter method: The HTTP method.
/// - parameter headers: The HTTP headers. `nil` by default.
///
/// - returns: The new `URLRequest` instance.
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)
}
}
各種請求方法
Alamofire中有四大類的請求方法,Data Request,Download Request,Stream Request,每類方法中又包含若干種不同的請求方法。由于這里的請求方法并沒有真正的執行網絡請求,只是對SessionManager中的請求方法的封裝,所有都是很簡單調用SessionManager方法而已。
Data Request
平時使用得最多的請求類型,可以創建一個簡單的網絡請求
///必填參數是實現了URLConvertible的類型,比如上面介紹的
String, URL,URLComponents 。
public func request(
_ url: URLConvertible,
method: HTTPMethod = .get,
parameters: Parameters? = nil,
encoding: ParameterEncoding = URLEncoding.default,
headers: HTTPHeaders? = nil)
-> DataRequest
///只需要一個URLRequestConvertible
public func request(_ urlRequest: URLRequestConvertible) -> DataRequest
Download Request
前兩個方法和上面的DataRequest的方法大同小異,第三個方法參數需要傳入一個Data,這個Data應該是用于斷點下載使用,上次任務取消后保留的Data。
public func download(
_ url: URLConvertible,
method: HTTPMethod = .get,
parameters: Parameters? = nil,
encoding: ParameterEncoding = URLEncoding.default,
headers: HTTPHeaders? = nil,
to destination: DownloadRequest.DownloadFileDestination? = nil)
-> DownloadRequest
public func download(
_ urlRequest: URLRequestConvertible,
to destination: DownloadRequest.DownloadFileDestination? = nil)
-> DownloadRequest
public func download(
resumingWith resumeData: Data,
to destination: DownloadRequest.DownloadFileDestination? = nil)
-> DownloadRequest