如何将未构建的Objective-C库项目导入SwiftUI项目并实现编译调用
Can I use an unbuilt Objective-C library in a new SwiftUI project? How?
Absolutely, this is totally feasible! I’ve done this many times before, so let me walk you through the exact steps to get your Objective-C library up and running in your SwiftUI project.
Step 1: First, build your Objective-C library to generate the .a file
Since you mentioned the library hasn’t been built yet, let’s fix that first:
- Open your Objective-C library project in Xcode.
- Double-check your build settings to avoid architecture mismatches later:
- Go to Build Settings > Architectures and make sure it’s set to match your SwiftUI project (usually
Standard Architectures (arm64, x86_64)works for both devices and simulators). - Pick your target build configuration: use
Debugfor testing,Releasefor production builds.
- Go to Build Settings > Architectures and make sure it’s set to match your SwiftUI project (usually
- Select a target (either a simulator like "iPhone 15" or "Any iOS Device (arm64)" for physical devices) and hit the Build button (or use the shortcut
Cmd+B). - Once built, find the
.afile: Right-click the library in the Products folder of your Objective-C project, selectShow in Finder—you’ll find it inside either theDebugorReleasefolder depending on your configuration.
Step 2: Integrate the Objective-C library into your SwiftUI project
Now let’s get the library into your new SwiftUI app:
- Add the library and header files
- Drag the
.afile from Finder into your SwiftUI project’s navigator. In the pop-up, checkCopy items if neededand make sure it’s linked to your main SwiftUI app target. - Do the same for all public header files of your Objective-C library. I recommend creating a dedicated folder (like
ObjCLibHeaders) in your SwiftUI project to keep these organized.
- Drag the
- Configure build settings for linking
- Go to your SwiftUI project’s Build Settings > Linking > Other Linker Flags, and add
-ObjC—this ensures Objective-C categories and static library code are properly linked (skip this and you’ll run into runtime errors!). - Under Build Settings > Search Paths > Header Search Paths, add the path to your header folder. If you used a relative path (like the folder you created in your project), use
$(SRCROOT)/ObjCLibHeadersand set the option toRecursive.
- Go to your SwiftUI project’s Build Settings > Linking > Other Linker Flags, and add
- Set up the bridging header
- Swift needs a bridging header to communicate with Objective-C. If you don’t have one yet, create a new
.hfile (e.g.,MySwiftUIApp-Bridging-Header.h). - In this file, import the public headers of your Objective-C library that you want to use in Swift:
#import "MyObjCLibPublicAPI.h" - Go back to Build Settings > Swift Compiler - General > Objective-C Bridging Header, and paste the path to your bridging header (e.g.,
$(SRCROOT)/MySwiftUIApp/MySwiftUIApp-Bridging-Header.h).
- Swift needs a bridging header to communicate with Objective-C. If you don’t have one yet, create a new
- Test it out!
- You’re ready to call Objective-C code from SwiftUI now. For example:
import SwiftUI struct ContentView: View { var body: some View { VStack(spacing: 20) { Text("Calling ObjC Library:") Text("Result: \(MyObjCLibHelper.shared.doSomethingCool())") } .padding() } } - Hit
Cmd+Rto run the app—if everything’s set up right, you’ll see the output from your Objective-C library.
- You’re ready to call Objective-C code from SwiftUI now. For example:
A few key notes to avoid headaches
- Make sure all classes and methods in your Objective-C library that you want to use in Swift are marked as public (i.e., declared in your public headers with
@interfaceand no private modifiers). - If your Objective-C library depends on other frameworks (like
UIKitor third-party libraries), add those dependencies to your SwiftUI project’s Build Phases > Link Binary With Libraries. - Whenever you update the Objective-C library code, rebuild it to get a new
.afile, then replace the old one in your SwiftUI project.
内容的提问来源于stack exchange,提问作者RemoterUser
相关产品推荐
相关产品推荐

