据我所知,下面代码中的可选失败块已经用?
打开,但xCode一直抱怨我的语法。如果仔细查看success
块,它看起来与failure
块完全相同,但不知何故,xCode没有标记success
块。我以前遇到过这个问题并设法通过构建清理来解决问题,但这次不是。有没有人遇到类似的问题?
public func authorizeLogin(token: String, success: (() -> ())?, failure: (() -> ())?) {
let path = "me/two-step/push-authentication"
let requestUrl = self.pathForEndpoint(path, withVersion: ServiceRemoteRESTApiVersion_1_1)
let parameters = [
"action" : "authorize_login",
"push_token" : token
]
api.POST(requestUrl,
parameters: parameters,
success: { (operation: AFHTTPRequestOperation!, response: AnyObject!) -> Void in
success?()
},
failure:{ (operation: AFHTTPRequestOperation!, error: NSError!) -> Void in
failure?()
})
}
以下是包含错误消息的屏幕截图:
value of optional type '()?' not unwrapped; did you mean to use '!' or '?'
答案 0 :(得分:0)
你的代码中有些东西看起来很可疑......你的failure
封闭和你的success
封闭似乎都试图自称。例如:
success: { (operation: AFHTTPRequestOperation!, response: AnyObject!) -> Void in
success?() // <- This appears to be trying to call
// the parameter itself
}
那永远不会奏效。这就像写作,
let add: () -> () = { add() }
,它给出错误error: variable used within its own initial value
我认为您收到的错误消息具有误导性,您需要做的是为不自称的success
和failure
参数编写闭包。