Objective-C到Swift的完成处理程序

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

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

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

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

我无法很好地理解Swift中的闭包语法。 任何帮助,将不胜感激!

你想为你的function这样的东西:

 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)来定义闭包 ,在这里用inputreplacearguments并返回outputs (很像定义函数的方式)。

在你的情况下,你的closures不会返回任何东西,所以我们将它们定义为:

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

(使用空元组()来表示没有任何东西被返回)

然后创build它们,并使用以下简写符号将它们传递给函数(如果已知闭包的参数types):

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

 closureArgument: { isRunning, error in } 

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

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

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

然后可以调用闭包,就像调用函数一样。

 closureArgument(isRunning: false, error: nil) 

我觉得这个网站是很好的参考闭包语法(可以解释它比我更好)。

我相信别人会给你非常详细的答案与示例代码,所以我只是提到这个以供将来参考:

如何在Swift中声明一个闭包?

从fuckingclosuresyntax.com或SFW版本goshdarnclosuresyntax.com 。

作为一个variables

 var closureName: (ParameterTypes) -> (ReturnType) 

作为一个可选variables

 var closureName: ((ParameterTypes) -> (ReturnType))? 

作为一个types别名

 typealias ClosureType = (ParameterTypes) -> (ReturnType) 

作为一个常数

 let closureName: ClosureType = { ... } 

作为函数调用参数

 func({(ParameterTypes) -> (ReturnType) in statements}) 

作为函数参数

 array.sort({ (item1: Int, item2: Int) -> Bool in return item1 < item2 }) 

作为一个隐含types函数参数

 array.sort({ (item1, item2) -> Bool in return item1 < item2 }) 

作为隐含返回types函数参数

 array.sort({ (item1, item2) in return item1 < item2 }) 

作为最后的function参数

 array.sort { (item1, item2) in return item1 < item2 } 

作为最后一个参数, 使用简写参数名称

 array.sort { return $0 < $1 } 

作为最后一个参数, 带有一个隐含的返回值

 array.sort { $0 < $1 } 

作为最后一个参数, 作为对现有函数的引用

 array.sort(<) 

作为具有显式捕获语义的函数参数:

 array.sort({ [unowned self] (item1: Int, item2: Int) -> Bool in return item1 < item2 }) 

作为具有显式捕获语义和推断参数/返回types的函数参数:

 array.sort({ [unowned self] in return item1 < item2 }) 

本网站并不打算成为closures所有可能用途的详尽清单。

为了理解闭包是如何工作的,我已经将闭包的定义分开了,而不是将其定义为内联到方法签名。 你可以做到以下几点:

 func start(startupHandler: (configuration: WTStartupConfiguration) -> Void, completion: (isRunning: Bool, error: NSError) -> Void ) { //Your code for start method } var startupHandlerClosure = { (configuration: WTStartupConfiguration) -> Void in //Your code for startupHandler closure } var completionClosure = { (isRunning: Bool, error: NSError) -> Void in //Your code for completion closure } //Method call start(startupHandlerClosure, completion: completionClosure) 

@ originaluser2打败了我..它提供了额外的代码的例子。