Can't use Swift classes inside Objective-C

前端 未结 25 2034
野趣味
野趣味 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:24

    Don't create the header file yourself. Delete the one you created.

    Make sure your Swift classes are tagged with @objc or inherit from a class that derives (directly or indirectly) from NSObject.

    Xcode won't generate the file if you have any compiler errors in your project - make sure your project builds cleanly.

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

    There is two condition,

    • Use your swift file in objective c file.
    • Use your objective c file in swift file.

    So, For that purpose, you have to follow this steps:

    • Add your swift file in an objective-c project or vice-versa.
    • Create header(.h) file.
    • Go to Build Settings and perform below steps with search,

      1. search for this text "brid" and set a path of your header file.
      2. "Defines Module": YES.
      3. "Always Embed Swift Standard Libraries" : YES.
      4. "Install Objective-C Compatibility Header" : YES.

    After that, clean and rebuild your project.

    Use your swift file in objective c file.

    In that case,First write "@objc" before your class in swift file.

    After that ,In your objective c file, write this,

      #import "YourProjectName-Swift.h"
    

    Use your objective c file in swift file.

    In that case, In your header file, write this,

      #import "YourObjective-c_FileName.h"
    

    I hope this will help you.

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

    Allow Xcode to do its work, do not add/create Swift header manually. Just add @objc before your Swift class ex.

    @objc class YourSwiftClassName: UIViewController
    

    In your project setting search for below flags and change it to YES (Both Project and Target)

    Defines Module : YES
    Always Embed Swift Standard Libraries : YES
    Install Objective-C Compatibility Header : YES
    

    Then clean the project and build once, after build succeed (it should probably) import below header file in your objective-c class .m file

    #import "YourProjectName-Swift.h" 
    

    Boooom!

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

    I had the same issue and it turned out special symbols in the module name are replaced by xcode (in my case dashes ended up being underscores). In project settings check "module name" to find the module name for your project. After that either use ModuleName-Swift.h or rename the module in settings.

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

    Using Swift Classes in Objective-C

    If you are going to import code within an App Target (Mixing Objective-C and Swift in one project) you should use the next import line #import "<#YourProjectName#>-Swift.h" to expose Swift code to Objective-C code [Mixing Swift and Objective-C code in a project]

    In this post I will describe how to import Swift static library to Objective-C code

    Objective-C consumer -> Swift static library

    Xcode version 10.2.1

    Create Swift static library

    Follow Create Swift static library with next additions:

    Expose Swift API. To use Swift's functions from Objective-C[About]

    After building you should find a <product_name>-Swift.h file that should be located into DerivedSources [File not found]

    Objective-C consumer with Swift static library

    Drag and drop the binary into the Xcode project[About]

    Link Library[Undefined symbols] [Link vs Embed]

    Project editor -> select a target -> General -> Linked Frameworks and Libraries -> add -> Add Others... -> point to `lib<product_name>.a` file
    //or
    Project editor -> select a target -> Build Phases -> Link Binary With Libraries -> add -> Add Others... -> point to `lib<product_name>.a` file
    

    Add Library Search paths[Library not found for] [Recursive path]

    Project editor -> select a target -> Build Settings -> Search Paths -> Library Search paths -> add path to the parent of `lib<product_name>.a` file
    

    Add Header Search Paths[Module not found] [Recursive path]

    Project editor -> select a target -> Build Settings -> Search Paths -> Header Search Paths -> add path to generated `<product_name>-Swift.h` file 
    

    Add empty .swift file to the Objective-C project.[Undefined symbols] When Xcode ask press Create Bridging Header(it will create module_name-Bridging-Header.h) and setup a path to this file in

    Project editor -> select a target -> Build Settings -> Swift Compiler - General -> Objective-C Bridging Header
    

    Import module to the Objective-C client code[File not found] [module_name]

    #import "module_name-Swift.h"
    

    More examples here

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

    The file is created automatically (talking about Xcode 6.3.2 here). But you won't see it, since it's in your Derived Data folder. After marking your swift class with @objc, compile, then search for Swift.h in your Derived Data folder. You should find the Swift header there.

    I had the problem, that Xcode renamed my my-Project-Swift.h to my_Project-Swift.h Xcode doesn't like "." "-" etc. symbols. With the method above you can find the filename and import it to a Objective-C class.

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