Possible or ObjC to call a Swift function?

I wrote a Swift function, but was unable to find a way to call if from Objective C. In the end I made it a static method on a new class - that worked.


Did I miss something? I tried adding "@objc" (not allowed) too. I cannot find this topic covered in the Swift-ObjC interworking book either.

Accepted Answer

Did I miss something?

Yes. It is clearly stated that Swift top-level functions are not available from Objective-C.

Swift Type Compatibility


You can assign (including passing as function/method argument) Swift functions to function pointer variables or block variables declared in Objective-C,

and call the function through those variables. But you cannot directly call Swift functions from Objective-C.

Problem for me was I didn't read the whole book (I did that a year or so ago) and this fact isn't obvious when looking elsewhere. In any case, thanks for your help!

Possible or ObjC to call a Swift function?
 
 
Q