0Pricing
Objective-C iOS Development for Legacy & Enterprise Apps · 课时

将 Objective-C 桥接到 Swift

了解如何使用桥接头文件向 Swift 公开展示 Objective-C 类和方法。

将 Objective-C 桥接到 Swift 是 CoddyKit 上的免费 Objective-C iOS Development for Legacy & Enterprise Apps 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Objective-C iOS Development for Legacy & Enterprise Apps 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Objective-C iOS Development for Legacy & Enterprise Apps 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Bridging Obj-C to Swift

Welcome! In modern iOS development, you'll often encounter projects that mix Swift and Objective-C. This is especially true for legacy apps or when integrating older libraries.

This lesson teaches you how to make your existing Objective-C code accessible and usable within a Swift project, a process known as bridging.

The Bridging Header

The key to using Objective-C code in Swift is the bridging header. This is a special Objective-C header file that tells the Swift compiler which Objective-C classes, categories, and protocols it should expose to your Swift code.

Think of it as a translator's dictionary for your project.

Xcode's Automatic Helper

When you add your first Objective-C file (.h or .m) to an existing Swift project, Xcode will often prompt you:

  • "Would you like to configure an Objective-C bridging header?"

If you click 'Create Bridging Header', Xcode automatically sets up the file and configures your project's build settings for you. It's usually named YourProjectName-Bridging-Header.h.

Manual Setup Steps

If Xcode doesn't prompt you, or if you need to create one later, you can do it manually:

  1. Create a new header file (File > New > File > Header File) and name it YourProjectName-Bridging-Header.h.
  2. In your project's Build Settings, search for 'Objective-C Bridging Header'.
  3. Set the path to your new header file (e.g., $(SRCROOT)/YourProjectName/YourProjectName-Bridging-Header.h).

Importing Obj-C Files

Once you have your bridging header (whether created automatically or manually), you simply import the Objective-C header files (.h files) of the classes you want to use in Swift.

You do this using the standard Objective-C #import directive inside your bridging header:

#import "MyObjectiveCClass.h"

Do NOT import Swift files into the bridging header!

Example: Simple Obj-C Class

Let's define a simple Objective-C class called MyLogger. We'll put its header in our bridging header.

// MyLogger.h

#import <Foundation/Foundation.h>

@interface MyLogger : NSObject

- (void)logMessage:(NSString *)message;
- (NSString *)getGreetingForName:(NSString *)name;

@end

This class has two methods: one to log a message and one to return a personalized greeting.

Implementation of MyLogger

Here's the implementation for our MyLogger class:

// MyLogger.m

#import "MyLogger.h"

@implementation MyLogger

- (void)logMessage:(NSString *)message {
    NSLog(@"[MyLogger] %@", message);
}

- (NSString *)getGreetingForName:(NSString *)name {
    return [NSString stringWithFormat:@"Hello, %@!", name];
}

@end

Remember, you'd add #import "MyLogger.h" to your YourProjectName-Bridging-Header.h.

Using Obj-C in Swift

Once MyLogger.h is imported into the bridging header, Swift can see and use MyLogger just like any other Swift class! Notice how Objective-C methods are automatically translated into a more Swift-like syntax.

Try running this example:

import Foundation

// Assume MyLogger is available via the bridging header
// In a real project, MyLogger.m and .h would be compiled

@objcMembers
class MyLogger: NSObject {
    func logMessage(_ message: String) {
        print("[MyLogger] \(message)")
    }
    
    func getGreetingForName(_ name: String) -> String {
        return "Hello, \(name)!"
    }
}

// --- Swift code using the bridged class ---
let logger = MyLogger()
logger.logMessage("This is a message from Swift!")

let greeting = logger.getGreetingForName("CoddyKit Learner")
print(greeting)

Type Mapping & Naming

Swift automatically maps many Objective-C types to their Swift equivalents:

  • NSString becomes String
  • NSArray becomes Array
  • NSDictionary becomes Dictionary
  • NSError becomes Error

Objective-C methods are also often renamed to fit Swift's naming conventions (e.g., - (void)logMessage:(NSString *)message; becomes func logMessage(_ message: String)).

Quick Check

You've learned about the bridging header. How does it enable Objective-C code to be used in Swift?

Recap: Bridging Success!

Great job! You've learned the fundamentals of bridging Objective-C code to Swift.

  • The bridging header is your gateway.
  • Xcode can auto-create it.
  • You #import Objective-C header files into it.
  • Swift automatically handles type mapping and naming conventions.

This skill is vital for working on mixed-language projects and integrating with existing Objective-C libraries!

常见问题解答

「将 Objective-C 桥接到 Swift」课时是免费的吗?

是的 — 「将 Objective-C 桥接到 Swift」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Objective-C iOS Development for Legacy & Enterprise Apps 课程的其余内容,请升级到 CoddyKit PRO。 Objective-C iOS Development for Legacy & Enterprise Apps 课程共包含 4 节课。

「将 Objective-C 桥接到 Swift」这节课中我会学到什么?

了解如何使用桥接头文件向 Swift 公开展示 Objective-C 类和方法。 你通过在浏览器中直接运行的动手代码来练习 Objective-C iOS Development for Legacy & Enterprise Apps,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Objective-C iOS Development for Legacy & Enterprise Apps 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Objective-C iOS Development for Legacy & Enterprise Apps 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「将 Objective-C 桥接到 Swift」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Objective-C iOS Development for Legacy & Enterprise Apps 课中编写并运行代码吗?

能。每节 Objective-C iOS Development for Legacy & Enterprise Apps 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 将 Objective-C 桥接到 Swift
  2. 将 Swift 桥接到 Objective-C
  3. 创建 Objective-C 框架
  4. 处理桥接过程中的可空性与类型映射
← 返回 Objective-C iOS Development for Legacy & Enterprise Apps