1

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

在此处输入图像描述

4

1 回答 1

1

您的代码中的某些内容看起来很可疑……您的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

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

于 2016-01-11T16:26:45.750 回答