Can't use Swift classes inside Objective-C

前端 未结 25 2031
野趣味
野趣味 2020-11-22 08:10

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

相关标签:
25条回答
  • 2020-11-22 08:31

    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.

    0 讨论(0)
  • 2020-11-22 08:33

    For Swift 5:

    1. Add the @objc keyword to your class and methods
    2. Add public keyword to your class and methods
    3. Let your class inherit from NSObject
    4. Build Project
    5. Put #import "MyProject-Swift.h" in your Objective-C file

      @objc
      public class MyClass: NSObject {
      
          @objc
          public func myMethod() {
      
          }
      }
      
    0 讨论(0)
  • 2020-11-22 08:33

    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

    import "Bedtime-Swift.h" <- inside objective-c file that needed to use that swift file.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 08:35

    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.

    0 讨论(0)
  • 2020-11-22 08:37

    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.

    0 讨论(0)
提交回复
热议问题