Alamofire.swift(一)-URLConvertibl

作者: Mr_Legend | 来源:发表于2016-11-08 17:36 被阅读448次

简单粗暴的学习方法:一行一行看,遇到问题,弄懂问题

首先来看4.0.1版本的代码行数:

6571

find . "(" -name "*.swift" ")" -print | xargs wc -l;
Paste_Image.png

先从Alamofire.swift文件入手:

接下来学习
URLConvertibleAFError

URL 和 URLComponents 的不同之处在于:URL component属性是 readwrite 的。它提供了安全直接的方法来修改URL的各个部分。

URLConvertible协议:
用于NSURL和String之间的相互转换,alamofire的思想还是很值得学习的:

这个protocol只定义了一个var, 遵循这个协议的类必须实现URLString返回String的这个功能.
接下来让所有可以转化为String的类全部遵循这个协议

public protocol URLConvertible {
    func asURL() throws -> URL
}

extension String: URLConvertible {
conforms to RFC 2396 or throws an `AFError`.
    public func asURL() throws -> URL {
        guard let url = URL(string: self) else { throw AFError.invalidURL(url: self) }
        return url
    }
}

extension URL: URLConvertible {
    public func asURL() throws -> URL { return self }
}

extension URLComponents: URLConvertible {
    public func asURL() throws -> URL {
        guard let url = url else { throw AFError.invalidURL(url: self) }
        return url
    }
}

AFError:

这里有一种枚举嵌套枚举的写法:


public enum AFError: Error {

    public enum ParameterEncodingFailureReason {
        case missingURL
        case jsonEncodingFailed(error: Error)
        case propertyListEncodingFailed(error: Error)
    }
    ...
    case invalidURL(url: URLConvertible)
    case parameterEncodingFailed(reason: ParameterEncodingFailureReason)
    case multipartEncodingFailed(reason: MultipartEncodingFailureReason)
    case responseValidationFailed(reason: ResponseValidationFailureReason)
    case responseSerializationFailed(reason: ResponseSerializationFailureReason)
}

AFError的Booleans扩展:用于对每一类错误进行返回匹配结果

// MARK: - Error Booleans

extension AFError {
    /// Returns whether the AFError is an invalid URL error.
    public var isInvalidURLError: Bool {
        if case .invalidURL = self { return true }
        return false
    }
    ...

AFError的Convenience扩

// MARK: - Convenience Properties

extension AFError {
    /// The `URLConvertible` associated with the error.
    public var urlConvertible: URLConvertible? {
        switch self {
        case .invalidURL(let url):
            return url
        default:
            return nil
        }
    }
    ...

针对AFError中的ParameterEncodingFailureReason类型错误进行扩展

extension AFError.ParameterEncodingFailureReason {
    var underlyingError: Error? {
        switch self {
        case .jsonEncodingFailed(let error), .propertyListEncodingFailed(let error):
            return error
        default:
            return nil
        }
    }
}

LocalizedError协议:描述了不同Error信息

Foundation:
/// Describes an error that provides localized messages describing why
/// an error occurred and provides more information about the error.
public protocol LocalizedError : Error {
    public var errorDescription: String? { get }
    public var failureReason: String? { get }
    public var recoverySuggestion: String? { get }
    public var helpAnchor: String? { get }
}

这里实现了errorDescription,localizedDescription是每一个错误类型中实现的的变量值,直接返回对应的错误原因

// MARK: - Error Descriptions

extension AFError: LocalizedError {
    public var errorDescription: String? {
        switch self {
        case .invalidURL(let url):
            return "URL is not valid: \(url)"
        case .parameterEncodingFailed(let reason):
            return reason.localizedDescription
        case .multipartEncodingFailed(let reason):
            return reason.localizedDescription
        case .responseValidationFailed(let reason):
            return reason.localizedDescription
        case .responseSerializationFailed(let reason):
            return reason.localizedDescription
        }
    }
}

extension AFError.ParameterEncodingFailureReason {
    var localizedDescription: String {
        switch self {
        case .missingURL:
            return "URL request to encode was missing a URL"
        case .jsonEncodingFailed(let error):
            return "JSON could not be encoded because of error:\n\(error.localizedDescription)"
        case .propertyListEncodingFailed(let error):
            return "PropertyList could not be encoded because of error:\n\(error.localizedDescription)"
        }
    }
}

相关文章

网友评论

    本文标题:Alamofire.swift(一)-URLConvertibl

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