Can't use Swift classes inside Objective-C

前端 未结 25 2030
野趣味
野趣味 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条回答
  • I have the same error: myProjectModule-Swift.h file not found", but, in my case, real reason was in wrong deployment target: "Swift is unavailable on OS X earlier than 10.9; please set MACOSX_DEPLOYMENT_TARGET to 10.9 or later (currently it is '10.7')" so, when I've changed deployment target to 10.9 - project had been compiled successfully.

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

    I spent about 4 hours trying to enable Swift in my Xcode Objective-C based project. My myproject-Swift.h file was created successfully, but my Xcode didn't see my Swift-classes. So, I decided to create a new Xcode Objc-based project and finally, I found the right answer! Hope this post will help someone :-)

    Step by step Swift integration for Xcode Objc-based project:

    1. Create new *.swift file (in Xcode) or add it by using Finder.
    2. Create an Objective-C bridging header when Xcode asks you about that.
    3. Implement your Swift class:

      import Foundation
      
      // use @objc or @objcMembers annotation if necessary
      class Foo {
          //..
      }
      
    4. Open Build Settings and check these parameters:

      • Defines Module : YES

        Copy & Paste parameter name in a search bar

      • Product Module Name : myproject

        Make sure that your Product Module Name doesn't contain any special characters

      • Install Objective-C Compatibility Header : YES

        Once you've added *.swift file to the project this property will appear in Build Settings

      • Objective-C Generated Interface Header : myproject-Swift.h

        This header is auto-generated by Xcode

      • Objective-C Bridging Header : $(SRCROOT)/myproject-Bridging-Header.h
    5. Import Swift interface header in your *.m file.

      #import "myproject-Swift.h"
      

      Don't pay attention to errors and warnings.

    6. Clean and rebuild your Xcode project.
    7. Profit!
    0 讨论(0)
  • 2020-11-22 08:15

    I had the same problem and finally it appeared that they weren't attached to the same targets. The ObjC class is attached to Target1 and Target2, the Swift class is only attached to the Target1 and is not visible inside the ObjC class.

    Hope this helps someone.

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

    Also probably helpful for those of you with a Framework target:

    The import statement of the auto-generated header file looks a bit different from app targets. In addition to the other things mentioned in other answers use

    #import <ProductName/ProductModuleName-Swift.h>
    

    instead of

    #import "ProductModuleName-Swift.h"
    

    as per Apples documentation on Mix & Match for framework targets.

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

    I just discovered that adding a directory of swift files to a project won't work. You need to create a group first for the directory, then add the swift files...

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

    Make sure your project defines a module and you have given a name to the module. Then rebuild, and Xcode will create the -Swift.h header file and you will be able to import.

    You can set module definition and module name in your project settings.

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