可选类型'()的值?'没有打开

时间:2016-01-11 12:22:06

标签: swift unwrap

据我所知,下面代码中的可选失败块已经用?打开,但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 '?'

enter image description here

1 个答案:

答案 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

我认为您收到的错误消息具有误导性,您需要做的是为不自称的successfailure参数编写闭包。