0

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.

1

1 Answer 1

2

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 (i.e. public/open classes inherited from NSObject or some other Cocoa classes)

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your answer. But neither of those statements work either, with "file not found" and "module not found" error messages. Do I need to copy the XCode generated Objective-C header file that is in the Swift framework somewhere?
@fishinear i actually just tried to add the Starscream library with SPM and the said method works just fine for me. If you have it in the form of a packaged framework, you just need to link it to the Xcode project. Dependency managers (usually) do it for you, but CocoaPods may be different in regards to this question
I am not using CocoaPods, I just dragged the Starscream framework into the Frameworks of my project in XCode. It works fine if I copy the Starscream-Swift.h header file from the framework into the project somewhere. But XCode does not seem to be able to find it in the framework itself.
If I use your sample project, then it works for me as well (after some tweaking). So probably it's some settings in my project that are the cause. Not sure what the difference is. "Starscream does not expose its classes to Obj-C runtime" - I discovered that after my struggle to get the header file working; I have now switched to Jetfire (fully Objective-C) instead. That seems to work fine, so the issue is not so urgent anymore. Thanks for your extensive efforts to support me in this.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.