1

I'm writing some Swift code that i'd like to use in Objective-C. The headers are all generated automatically but when using closures (that become blocks in objective-c), the variable names are missing.

For Example:

 @objc public func doSomething(success: (result: String) ->())

becomes

 -(void)doSomething:(NSString * _Nonnull)success;

Where i would have expected it to be:

 -(void)doSomething:(NSString * result)success;

Is this an Xcode bug or is there a way to specify what the variable should be named?

6
  • did you check developer.apple.com/swift/blog/?id=25 ? Commented Dec 12, 2015 at 17:05
  • and please, finish your swift func doSomething definition .... Commented Dec 12, 2015 at 17:11
  • what do you mean finish the function? i.e. add brackets? Commented Dec 13, 2015 at 16:38
  • Also, the doc you referenced is for going Objective-C -> Swift not the other way around Commented Dec 13, 2015 at 16:40
  • no, no! function and closure in Swift has the same type!! so i recommend you to use function, and check how you will see it in Objective C. closure is like anonymous function in Swift Commented Dec 13, 2015 at 16:46

1 Answer 1

0

function and closure in Swift are the same type, you can try something like

// what you have
func boo(mf: String->Void) { mf("alfa") }
boo { (str) -> Void in
    print(str)
} // "alfa"

// try this
func foo(str: String) { print(str + " beta") }
// and see the signature in Objective C
boo(foo)

/*
alfa
alfa beta
*/
Sign up to request clarification or add additional context in comments.

1 Comment

The output for this is the same: - (void)boo:(void (^ __nonnull)(NSString * __nonnull))mf;

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.