类型为 () -> () 的 iOS Swift 函数

标签 ios swift xcode

我无法使用 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/

相关文章:

ios - 快速将本地化字符串添加到 UITextView

ios - 没有这样的模块 `Quick` 使用 cocoapods

ios - 如何在swift中启动一个全局线程

ios - 签名无效 - 代码对象根本没有签名

ios - UITableView 自定义拖放(Trello 风格)

ios - 在 iOS 4.0 及以上版本扫描和切换 Wifi 网络

Swift:协议(protocol)中属性的默认值

ios - 如何使用 UITabBarController (Swift) 在选项卡之间发送 NSNotifications

Xcode 4.2 + c++0x/libc++ 找不到 iostream

ios - OCUnit 已弃用。将 Xcode 升级到 5.1 后出错