引言
在前面,我们提到了DataResponse
多了一个result
属性,该属性的存在,即是存储了序列化后的数据。result
属性的类型是Result
,这篇文章,我们就来说说Result
。
在Alamofire
中,我们使用了Response
来描述请后的结果,这些结果,经过Alamofire
的序列化等特殊处理,得到我们想要的结果。因为得到的数据类型各有不同,我们在序列化后的结果中,如何获取这些类型不同的数据?
现在,我们需要封装一个对象,这个对象可以表示出我们想要的任何结果,因此,Result
出现了。
Result
定义
常规,看看代码:
/// Used to represent whether a request was successful or encountered an error.
///
/// - success: The request and all post processing operations were successful resulting in the serialization of the
/// provided associated value.
///
/// - failure: The request encountered an error resulting in a failure. The associated values are the original data
/// provided by the server as well as the error that caused the failure.
public enum Result<Value> {
case success(Value)
case failure(Error)
/// Returns `true` if the result is a success, `false` otherwise.
public var isSuccess: Bool {
switch self {
case .success:
return true
case .failure:
return false
}
}
/// Returns `true` if the result is a failure, `false` otherwise.
public var isFailure: Bool {
return !isSuccess
}
/// Returns the associated value if the result is a success, `nil` otherwise.
public var value: Value? {
switch self {
case .success(let value):
return value
case .failure:
return nil
}
}
/// Returns the associated error value if the result is a failure, `nil` otherwise.
public var error: Error? {
switch self {
case .success:
return nil
case .failure(let error):
return error
}
}
}
因为,在网络请求的结果中,不外乎就2种结果,要么成功、要么失败,所以,在这里,Alamofire
使用了一个枚举来定义Result
。
在Result
定义中,不论是成功选项还是失败选项,都为其子选项做了值关联的,另外,Result
还是个泛型。
使用
先来看下这个函数:
/// Adds a handler to be called once the request has finished.
///
/// - parameter options: The JSON serialization reading options. Defaults to `.allowFragments`.
/// - parameter completionHandler: A closure to be executed once the request has finished.
///
/// - returns: The request.
@discardableResult
public func responseJSON(
queue: DispatchQueue? = nil,
options: JSONSerialization.ReadingOptions = .allowFragments,
completionHandler: @escaping (DataResponse<Any>) -> Void)
-> Self
{
return response(
queue: queue,
responseSerializer: DataRequest.jsonResponseSerializer(options: options),
completionHandler: completionHandler
)
}
这个函数的目的是把请求成功的结果序列化为了JSON
数据,completionHandler
的参数类型为DataResponse<Any>
,Any
就会传递给Result
。
有的人可能会问:数据既然已经解析成了JSON
,为什么还要返回Any
类型?因为,解析后的数据有可能是数组、也有可能是字典,如果解析后是其他格式的数据,那么解析后就会抛出异常。
从上面Result
的定义,我们可以看到,Result
还有一些其他的属性,主要是给我们使用方便提供的。
扩展
前面我们也有接触过,为了打印出更详细的信息,扩展了Result
,实现了CustomStringConvertible
和CustomDebugStringConvertible
协议:
// MARK: - CustomStringConvertible
extension Result: CustomStringConvertible {
/// The textual representation used when written to an output stream, which includes whether the result was a
/// success or failure.
public var description: String {
switch self {
case .success:
return "SUCCESS"
case .failure:
return "FAILURE"
}
}
}
// MARK: - CustomDebugStringConvertible
extension Result: CustomDebugStringConvertible {
/// The debug textual representation used when written to an output stream, which includes whether the result was a
/// success or failure in addition to the value or error.
public var debugDescription: String {
switch self {
case .success(let value):
return "SUCCESS: \(value)"
case .failure(let error):
return "FAILURE: \(error)"
}
}
}
总的来说,Result
的封装还是挺简单的,很简单的封装,让我们使用起来特别爽。
常规打广告系列:
简书:Alamofire(七)-- 结果封装Result
掘金:Alamofire(七)-- 结果封装Result
小专栏:Alamofire(七)-- 结果封装Result
网友评论