I tried to declare a closure matching the following Objective-C block:
typedef void(^TyphoonDefinitionBlock)(TyphoonDefinition *definition);
like this:
var config: TyphoonDefinitionBlock = { (definition: TyphoonDefinition) in
definition.injectProperty("quest", with: nil)
}
. . . and got the following error. (see image).
What's the correct way to do this?
To simulate it, check your Project-Swift. h file. @objc class TestClass: NSObject { @objc var cancelledStateColorHandler: ((NSColor) -> NSColor)? }
You can use Objective-C and Swift files together in a single project, no matter which language the project used originally. This makes creating mixed-language app and framework targets as straightforward as creating an app or framework target written in a single language.
A closure is said to escape a function when the closure is passed as an argument to the function, but is called after the function returns. When you declare a function that takes a closure as one of its parameters, you can write @escaping before the parameter's type to indicate that the closure is allowed to escape.
You need to declare definition
as an ImplicitlyUnwrappedOptional (TyphoonDefinition!
) because in objective-C it is a pointer that can be nil.
Normal variables (and constants) in swift cannot be nil. They must contain a value.
I'm using typealias, taken from http://berzniz.com/post/87924122326/notes-from-coding-in-swift typealias resultBlock = (success: Bool, result: AnyObject!) -> Void
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With