Error
協(xié)議是用來進行錯誤處理的。
源碼如下:
public protocol Error {
}
extension Error {
//錯誤的本地化描述
public var localizedDescription: String { get }
}
extension Error where Self.RawValue : SignedInteger {
}
extension Error where Self.RawValue : UnsignedInteger {
}
可以看到,標準庫中只提供了一個localizedDescription
只讀屬性呕屎,用來描述錯誤的具體信息磺陡。除此之外趴梢,并沒有其他的方法。
Apple官方推薦使用枚舉類型币他,具體說明如下坞靶。
Swift's enumerations are well suited to represent simple errors. Create an enumeration that conforms to the Error protocol with a case for each possible error. If there are additional details about the error that could be helpful for recovery, use associated values to include that information.
關(guān)于錯誤處理,我自己對這方面的了解的也不夠深蝴悉。這里我給大家介紹一下Alamofire
的AFError
類彰阴,間接的來了解一下Error
協(xié)議的用法。
定義
public enum AFError: Error {
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)
. . .
}
public enum ResponseValidationFailureReason {
case dataFileNil
case dataFileReadFailed(at: URL)
case missingContentType(acceptableContentTypes: [String])
. . .
}
public enum ResponseSerializationFailureReason {
case inputDataNil
case inputDataNilOrZeroLength
case inputFileNil
. . .
}
case invalidURL(url: URLConvertible)
case parameterEncodingFailed(reason: ParameterEncodingFailureReason)
case multipartEncodingFailed(reason: MultipartEncodingFailureReason)
case responseValidationFailed(reason: ResponseValidationFailureReason)
case responseSerializationFailed(reason: ResponseSerializationFailureReason)
}
每個case
分別對應(yīng)一種錯誤類型拍冠,并關(guān)聯(lián)一個Reason
尿这。
嵌套4個Reason
枚舉簇抵,來對錯誤的原因再次進行分類。不得不說代碼很有邏輯感射众,也寫的很好看碟摆。
錯誤描述
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)"
}
}
}
extension AFError.MultipartEncodingFailureReason {
var localizedDescription: String {
switch self {
case .bodyPartURLInvalid(let url):
return "The URL provided is not a file URL: \(url)"
case .bodyPartFilenameInvalid(let url):
return "The URL provided does not have a valid filename: \(url)"
case .bodyPartFileNotReachable(let url):
return "The URL provided is not reachable: \(url)"
. . .
}
}
}
extension AFError.ResponseSerializationFailureReason {
var localizedDescription: String {
switch self {
case .inputDataNil:
return "Response could not be serialized, input data was nil."
case .inputDataNilOrZeroLength:
return "Response could not be serialized, input data was nil or zero length."
case .inputFileNil:
return "Response could not be serialized, input file was nil."
. . .
}
}
}
extension AFError.ResponseValidationFailureReason {
var localizedDescription: String {
switch self {
case .dataFileNil:
return "Response could not be validated, data file was nil."
case .dataFileReadFailed(let url):
return "Response could not be validated, data file could not be read: \(url)."
case .missingContentType(let types):
return (
"Response Content-Type was missing and acceptable content types " +
"(\(types.joined(separator: ","))) do not match \"*/*\"."
)
. . .
}
}
}
使用localizedDescription
屬性,對每一個Reason
的分支叨橱,進行詳細的描述典蜕。
當然了,AFError
并不是只有這些內(nèi)容罗洗,還有很多方便我們使用的擴展等愉舔,由于它并不是我這篇文章的主要討論方向,就不做更多介紹了栖博。 有興趣的可以去看看下面這個參考鏈接屑宠。
http://www.reibang.com/p/99e6ba32f244