Swift Error 的介绍和使用

同步我的简书

  鶸学python看到错误处理这一章的时候,就想对比着swift来研究一下。

  本文前面是Error苹果文档的介绍,然后对Alamofire中的使用作简单介绍作为实践;后面还有关于fatalError和高级语言错误处理机制的理解。


Error苹果文档

Swift中的Error长这个样子

public protocol Error {
}

extension Error {
}

extension Error where Self.RawValue : SignedInteger {
}

extension Error where Self.RawValue : UnsignedInteger {
}
复制代码

苹果文档对Error的介绍

A type representing an error value that can be thrown. Any type that declares conformance to the Error protocol can be used to represent an error in Swift’s error handling system. Because the Error protocol has no requirements of its own, you can declare conformance on any custom type you create.

  从介绍上可以看出遵守这个Error协议,就可以将错误抛出和捕获,而且任何自定义的类型都可以遵守此Error协议。

  文档中举了两个例子,第一个是自定义枚举遵守Error来对Error进行分类,下面我们着重看下苹果爸爸给出的第二个例子:用一个遵守Error协议的结构体来获取更多的错误信息。

struct XMLParsingError: Error {
    enum ErrorKind {
        case invalidCharacter
        case mismatchedTag
        case internalError
    }

    let line: Int
    let column: Int
    let kind: ErrorKind
}

func parse(_ source: String) throws -> XMLDoc {
    // ...
    throw XMLParsingError(line: 19, column: 5, kind: .mismatchedTag)
    // ...
}
复制代码

例子中创建了一个遵守Error协议的结构体 XMLParsingError,带有三个属性来记录解析XML文件时出错的错误类型和行列信息。这样在执行parse方法的时候,就可以捕获到可能的出现的错误信息

do {
    let xmlDoc = try parse(myXMLData)
} catch let e as XMLParsingError {
    print("Parsing error: \(e.kind) [\(e.line):\(e.column)]")
} catch {
    print("Other error: \(error)")
}
// Prints "Parsing error: mismatchedTag [19:5]"
复制代码

Alamofire中的错误示例

  刻意看了下Alamofire中的错误处理,它用单独的一个文件AFError来管理使用中的错误,其中自定义了枚举

public enum AFError: Error {
                                encoding process.
    public enum ParameterEncodingFailureReason {
        case missingURL
        case jsonEncodingFailed(error: Error)
        case propertyListEncodingFailed(error: Error)
    }

    
    public enum MultipartEncodingFailureReason {
        case bodyPartURLInvalid(url: URL)
        case bodyPartFilenameInvalid(in: URL)
        case bodyPartFileNotReachable(at: URL)
        case bodyPartFileNotReachableWithError(atURL: URL, error: Error)
        case bodyPartFileIsDirectory(at: URL)
        case bodyPartFileSizeNotAvailable(at: URL)
        case bodyPartFileSizeQueryFailedWithError(forURL: URL, error: Error)
        case bodyPartInputStreamCreationFailed(for: URL)

        case outputStreamCreationFailed(for: URL)
        case outputStreamFileAlreadyExists(at: URL)
        case outputStreamURLInvalid(url: URL)
        case outputStreamWriteFailed(error: Error)

        case inputStreamReadFailed(error: Error)
    }

    
    public enum ResponseValidationFailureReason {
        case dataFileNil
        case dataFileReadFailed(at: URL)
        case missingContentType(acceptableContentTypes: [String])
        case unacceptableContentType(acceptableContentTypes: [String], responseContentType: String)
        case unacceptableStatusCode(code: Int)
    }

    
    public enum ResponseSerializationFailureReason {
        case inputDataNil
        case inputDataNilOrZeroLength
        case inputFileNil
        case inputFileReadFailed(at: URL)
        case stringSerializationFailed(encoding: String.Encoding)
        case jsonSerializationFailed(error: Error)
        case propertyListSerializationFailed(error: Error)
    }

    case invalidURL(url: URLConvertible)
    case parameterEncodingFailed(reason: ParameterEncodingFailureReason)
    case multipartEncodingFailed(reason: MultipartEncodingFailureReason)
    case responseValidationFailed(reason: ResponseValidationFailureReason)
    case responseSerializationFailed(reason: ResponseSerializationFailureReason)
}
复制代码

以此将使用中出现的错误进行分类。

  不仅如此,作者还将AFError进行了扩展,增加部分判断的方法,比如判断是否是无效的URL:

// 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
    }
}
复制代码

同时拓展中也遵守了LocalizedError协议来重写errorDescription属性,并且对已经分类enum的自定义错误类型进行拓展重写了属性localizedDescription,这样就构成了项目中完整的错误处理机制。比如在Alamofire编码解析方法方法encode中,有这么一段代码

do {
            let data = try JSONSerialization.data(withJSONObject: jsonObject, options: options)

            if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
                urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
            }

            urlRequest.httpBody = data
        } catch {
            throw AFError.parameterEncodingFailed(reason: .jsonEncodingFailed(error: error))
        }
复制代码

其中就是对于JSON序列化时,通过 do { try } catch 的方式来捕获错误,如果捕获到,将抛出已经自定义好的 AFError.parameterEncodingFailed(reason: .jsonEncodingFailed(error: error)) 类型错误。


fatalError

还有一个错误相关的是 fatalError, 苹果对其的描述:

Unconditionally prints a given message and stops execution.

定义:

/// - Parameters:
///   - message: The string to print. The default is an empty string.
///   - file: The file name to print with `message`. The default is the file
///     where `fatalError(_:file:line:)` is called.
///   - line: The line number to print along with `message`. The default is the
///     line number where `fatalError(_:file:line:)` is called.
public func fatalError(_ message: @autoclosure () -> String = default, file: StaticString = #file, line: UInt = #line) -> Never
复制代码

这个我们估计最常见的地方就是在 init(coder: NSCoder) 中看到它,它的作用如苹果描述的,无条件的打印出给定的信息同时终止程序。按照喵神的描述,fatalError的存在意义是:

在调试时我们可以使用断言来排除类似这样的问题,但是断言只会在 Debug 环境中有效,而在 Release 编译中所有的断言都将被禁用。在遇到确实因为输入的错误无法使程序继续运行的时候,我们一般考虑以产生致命错误 fatalError 的方式来终止程序。

而实际开发中喵神给出两个使用的场景:

  1. 父类中的某些方法,不想让别人调用,可以在方法中加上fatalError,这样子类如果想到用必须重写
  2. 对于其他一切我们不希望别人随意调用,但是又不得不去实现的方法,我们都应该使用 fatalError 来避免任何可能的误会。

关于第二点就是我们常见到的在重写UIView init(frame:) 时,Xcode会提示需要显示重写 init(coder: NSCoder),并且给出默认实现的原因:

required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
复制代码

UIView遵守了协议 NSCoding,此协议要求实现 init(coder: NSCoder)

public init?(coder aDecoder: NSCoder) // NS_DESIGNATED_INITIALIZER
复制代码

详细的场景介绍请看喵神的文章


关于错误处理机制

  鶸之前也想过错误处理机制的意义,返回一个状态码不就好了。比如我们打开一个文件,成功了返回一个整数的描述符,错误了就返回-1;或者我们请求联网,如果参数有问题,会和后台约定一个数字作为错误码,也都很好。然而只是单纯的用错误码会面临两个问题:

  1. 用错误码来表示是否出错十分不便,因为函数本身应该返回的正常结果和错误码混在一起,造成调用者必须用大量的代码来判断是否出错。
  2. 一旦出错,还要一级一级上报,直到某个函数可以处理该错误(比如,给用户输出一个错误信息)。

高级语言的错误处理机制就解决了这些问题。

喵神FATALERROR

苹果文档Error

廖雪峰python

转载于:https://juejin.im/post/5ad4774c5188255cb32e9b6f

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值