我无法使用 xCode 在 Swift 中创建 () -> () 类型的函数...
到目前为止我在
func successBlock() -> Void {
//code
return ()
}
但这只是 () 而不是 () -> ()
我希望这个函数适合类型 () -> () 因为这个函数:
func loginToFacebookWithSuccess(callingViewController: UIViewController, successBlock: () -> (), andFailure failureBlock: (NSError?) -> ()) {
要将 successBlock() 函数作为变量传递给那里。
我对 failureBlock 有几乎相同的问题
还是我走错了路?
谢谢你,spinhaxo
最佳答案
successBlock
是一个函数。
你需要闭包。
let successBlock: () -> () = {
// do something here...
}
let failureBlock: (NSError?) -> () = { error in
// do something with the error...
}
如果你不想存储它们,你可以像这样在调用方法时定义它们:
loginToFacebookWithSuccess(
callingController,
successBlock: {
// do something here...
}, andFailure: { error in
// do something with the error...
}
)
关于类型为 () -> () 的 iOS Swift 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38634130/