简体   繁体   中英

How to use Swift framework in Objective-c app

I would like to use a Swift library in my Objective-C IOS app. I have the Swift library setup as a separate project, which builds fine in XCode. I can drag the generated Swift framework from the Swift project into the "Frameworks, Libraries and embedded content" list of my Objective-C project target. It appears with an "Embed & Sign" label.

The Apple description here: https://developer.apple.com/documentation/swift/importing-swift-into-objective-c states that

You can work with types declared in Swift from within the Objective-C code in your project by importing an Xcode-generated header file. This file is an Objective-C header that declares the Swift interfaces in your target, and you can think of it as an umbrella header for your Swift code. You don't need to do anything special to create the generated header—just import it to use its contents in your Objective-C code.

When I look inside the swift .framework file, I can see the header file there. But when I import it in one of my Objective-c.m files, then the compiler says that the file is not found. I have tried both the

#import "Starscream-Swift.h"

and the

#import <Starscream/Starscream-Swift.h>

syntax.

How can I convince XCode to use the header file from the Swift framework? Do I need to copy that header somewhere, maybe? And what else do I need to think about to use a Swift library from an Objective-c IOS app?

I am using XCode version 13.4.1. By the way, the Swift library I am trying to use is Starstream.

Xcode-generated header file is required when your own project has Objective-C and Swift classes mixed. When dealing with frameworks you either include the framework's umbrella header like this:

#import "SwiftFramework/SwiftFramework.h"

Or, more preferably, you import it with @import expression:

@import SwiftFramework;

Be advised, that only part that is exposed to Objective-C runtime is accessible from this framework (ie public/open classes inherited from NSObject or some other Cocoa classes)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM