I try to integrate Swift
code in my app.My app is written in Objective-C
and I added a Swift
class. I\'ve done everything described he
XCode 11.3.1:
When I want to use an Swift inner class in a objc code, it does not compile for ther error "undefined symbol"(for bother inner class and outer class), I checked the generated "-swift.h" header and both classes are there.
After trying for hours I convert the inner class to a normal class and it compiles.
I clean the project, delete the DerivedData folder and it compiles.
For Swift 5:
@objc
keyword to your class and methodspublic
keyword to your class and methodsNSObject
Put #import "MyProject-Swift.h"
in your Objective-C file
@objc
public class MyClass: NSObject {
@objc
public func myMethod() {
}
}
I didnt have to change any settings in the build or add @obj to the class.
All I had to do was to create bridge-header which was automatically created when I created Swift classes into Objective-c project. And then I just had to do
Just include #import "myProject-Swift.h" in .m or .h file
P.S You will not find "myProject-Swift.h" in file inspector it's hidden. But it is generated by app automatically.
My issue was that the auto-generation of the -swift.h file was not able to understand a subclass of CustomDebugStringConvertible. I changed class to be a subclass of NSObject instead. After that, the -swift.h file now included the class properly.
well, after reading all the comments and trying and reading and trying again, I managed to include swift classes into my Big obj-c project. So, thanks for all the help. I wanted to share one tip that helped me understand the process better. In the .m class, went to the import line of the swift target name #import "myTargetName-Swift.h" and clicked the key:
command + mouse click -> Jump to definition
There you can see all the translation from swift to obj-c and ther you will find the various functions re-declared in obj-c. Hope this tip will help you as much as it helped me.