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

Completion Handlers for Objective-C to 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 中的闭包语法。如有任何帮助,我们将不胜感激!

你的函数需要这样的东西:

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 替换为您的输入,将 return 替换为您的 [=20] =](很像函数的定义方式)。

在你的例子中,你的闭包没有 return 任何东西,所以我们将它们定义为:

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

(其中空元组 () 用于表明没有 returned)

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

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)

我发现 this site 非常适合作为闭包语法的参考(并且可能比我解释得更好)。

为了让您了解闭包的工作原理,我将闭包的定义分开,而不是将其内联定义到方法签名中。您可以执行以下操作:

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 打败了我。他提供了带有附加代码的示例。

我相信其他人会通过示例代码为您提供非常详细的答案,所以我只提一下以供将来参考:

如何在 Swift 中声明闭包?

来自 fuckingclosuresyntax.com or the SFW version goshdarnclosuresyntax.com.

As a variable:

var closureName: (ParameterTypes) -> (ReturnType)

As an optional variable:

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

As a type alias:

typealias ClosureType = (ParameterTypes) -> (ReturnType)

As a constant:

let closureName: ClosureType = { ... }

As an argument to a function call:

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

As a function parameter:

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

As a function parameter with implied types:

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

As a function parameter with implied return type:

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

As the last function parameter:

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

As the last parameter, using shorthand argument names:

array.sort { return [=19=] <  }

As the last parameter, with an implied return value:

array.sort { [=20=] <  }

As the last parameter, as a reference to an existing function:

array.sort(<)

As a function parameter with explicit capture semantics:

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

As a function parameter with explicit capture semantics and inferred parameters / return type:

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

This site is not intended to be an exhaustive list of all possible uses of closures.