用于Swift的Objective-C的完成处理程序 [英] Completion Handlers for Objective-C to Swift

查看:68
本文介绍了用于Swift的Objective-C的完成处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在从Objective-C到Swift重写一个项目.该项目的大部分工作都已完成,但是我在翻译具有完成处理程序的方法时遇到了问题.我已经查看了文档,但是仍然遇到问题.该方法是:

I am currently rewriting a project from the Objective-C to Swift. Most of the project is done, but I am having problems translating a method that has a completion handler. I have reviewed the documentation, but I am still having problems. The method is:

- (void)start:(void ( ^ ) ( WTStartupConfiguration *configuration ))startupHandler 
completion:(void ( ^ ) ( BOOL isRunning , NSError *error ))completionHandler

在Objective-C中,我可以简单地写为:

In objective-C, I would simply write is as:

[self.architectView start:^(WTStartupConfiguration *configuration)
 { } completion:^(BOOL isRunning, NSError *error) {}}];

我无法很好地掌握Swift中的闭包语法.任何帮助将不胜感激!

I cannot get a good grasp on closure syntax in Swift. Any help would be appreciated!

推荐答案

您想要这样的功能:

func start(startupHandler:(configuration: WTStartupConfiguration)->(), completion:(isRunning:Bool, error:NSError?)->()) {

    let configuration = WTStartupConfiguration() // create your configuration

    startupHandler(configuration:configuration) // call your startup handler closure

    ... // do some stuff

    let isRunning = false // replace with actual logic
    let error = nil // replace with your actual error detection

    completion(isRunning: isRunning, error: error) // call completion closure
}

然后您可以这样称呼它:

start(
    { configuration in

        // do something with your configuration

    }, completion: {isRunning, error in

        // do something with your isRunning & error.

})


闭包是用语法(arguments) -> (returns) 定义的,您可以在其中用输入替换arguments,并用outputs返回(很像定义函数的方式).


Closures are defined with the syntax (arguments) -> (returns), where you replace arguments with your inputs and returns with your outputs (much like how a function is defined).

在您的情况下,闭包不返回任何内容,因此我们将其定义为:

In your case, your closures don't return anything, so we define them like:

(isRunning:Bool, error:NSError?) -> ()

(其中一个空元组()用于表示未返回任何内容)

(where an empty tuple () is used to show that nothing is returned)

然后创建它们,并使用以下缩写表示法将它们传递给函数(如果已知闭包的参数类型):

You then create them, and pass them into functions (if the argument types of the closure are known) using the short-hand notation of:

closureArgument: { (arguments here, without types as they're known) in

}

closureArgument: { isRunning, error in 

}

将它们传递给函数的更正式的方法是:

The more formal way of passing them into a function is:

closureArgument: { (arguments with types) -> (returns with types) in

}

closureArgument: { (isRunning:Bool, error:NSError?) -> () in 

}

然后,您可以使用与调用函数几乎相同的方式来调用闭包.

You can then invoke the closure in pretty much the same way that you call functions.

closureArgument(isRunning: false, error: nil)

我发现此网站非常适合作为闭包语法的参考(并且可能比它解释得更好)我).

I find this site is great to refer to for closure syntax (and can probably explain it better than me).

这篇关于用于Swift的Objective-C的完成处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆