Can't use Swift classes inside Objective-C

前端 未结 25 2045
野趣味
野趣味 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: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 -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.a` file
    //or
    Project editor -> select a target -> Build Phases -> Link Binary With Libraries -> add -> Add Others... -> point to `lib.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.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 `-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

提交回复
热议问题