gpt4 book ai didi

swift - 泛型的尾随闭包?

转载 作者:行者123 更新时间:2023-11-28 12:57:26 26 4
gpt4 key购买 nike

您好,我正在尝试理解以下来自 Alamofire 的代码。如何使用“{}”初始化结构我知道您可以使用尾随闭包调用闭包。我知道我完全错过了一些东西,但是什么?

extension Request {
public func responseObject<T: ResponseObjectSerializable>(completionHandler: Response<T, NSError> -> Void) -> Self {

let responseSerializer = ResponseSerializer<T, NSError> { // What is this?
request, response, data, error in
guard error == nil else { return .Failure(error!) }

let JSONResponseSerializer = Request.JSONResponseSerializer(options: .AllowFragments)
let result = JSONResponseSerializer.serializeResponse(request, response, data, error)

switch result {
case .Success(let value):
if let
response = response,
responseObject = T(response: response, representation: value)
{
return .Success(responseObject)
} else {
let failureReason = "JSON could not be serialized into response object: \(value)"
let error = Error.errorWithCode(.JSONSerializationFailed, failureReason: failureReason)
return .Failure(error)
}
case .Failure(let error):
return .Failure(error)
}
}

return response(responseSerializer: responseSerializer, completionHandler: completionHandler)
}
}

Alamofire 的 ResponseSerializer 结构体

public struct ResponseSerializer<Value, Error: ErrorType>: ResponseSerializerType {
/// The type of serialized object to be created by this `ResponseSerializer`.
public typealias SerializedObject = Value

/// The type of error to be created by this `ResponseSerializer` if serialization fails.
public typealias ErrorObject = Error

/**
A closure used by response handlers that takes a request, response, data and error and returns a result.
*/
public var serializeResponse: (NSURLRequest?, NSHTTPURLResponse?, NSData?, NSError?) -> Result<Value, Error>

/**
Initializes the `ResponseSerializer` instance with the given serialize response closure.

- parameter serializeResponse: The closure used to serialize the response.

- returns: The new generic response serializer instance.
*/
public init(serializeResponse: (NSURLRequest?, NSHTTPURLResponse?, NSData?, NSError?) -> Result<Value, Error>) {
self.serializeResponse = serializeResponse
}
}

最佳答案

您的问题可以大大减少(并且应该减少)。下面是ResponseSerializer的相关声明:

public struct ResponseSerializer<Value, Error: ErrorType>: ResponseSerializerType {
public init(serializeResponse: (NSURLRequest?, NSHTTPURLResponse?, NSData?, NSError?) -> Result<Value, Error>) {
self.serializeResponse = serializeResponse
}
}

所以这个初始化器,init(serializeResponse:),接受一个参数——一个接受四个参数并返回一个参数(指定类型)的函数。

因此,我们可以这样初始化:

func f (request:NSURLRequest?, response:NSHTTPURLResponse?, data:NSData?, error:NSError?) -> Result<Value, Error>)  {
guard error == nil else { return .Failure(error!)
}
let responseSerializer = ResponseSerializer<T, NSError>(serializeResponse:f)

然而,这可以被压缩。我们真的不需要函数 f 做任何其他事情,所以我们可以将它作为匿名函数提供;它不需要名称或完整声明。此外,匿名函数有一个“捷径”规则,即如果匿名函数是函数的最后参数,则可以按字面意思在函数的右括号之后提供它, 省略了参数名称。如果函数没有其他参数,它的括号可以完全省略

好吧,这个 init 就是这样一个函数——它把一个函数作为它的最后一个(也是唯一的)参数——所以这正是有问题的代码所做的:

let responseSerializer = ResponseSerializer<T, NSError> { 
request, response, data, error in
guard error == nil else { return .Failure(error!)
}

关于swift - 泛型的尾随闭包?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34696042/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com