การเชื่อม Objective-C กับ Swift
ทำความเข้าใจวิธีเปิดให้คลาสและเมธอดของ Objective-C ใช้งานจาก Swift ผ่านส่วนหัวสำหรับการเชื่อมภาษา
การเชื่อม Objective-C กับ Swift เป็นบทเรียน Objective-C iOS Development for Legacy & Enterprise Apps ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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:
- Create a new header file (File > New > File > Header File) and name it
YourProjectName-Bridging-Header.h. - In your project's Build Settings, search for 'Objective-C Bridging Header'.
- 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:
NSStringbecomesStringNSArraybecomesArrayNSDictionarybecomesDictionaryNSErrorbecomesError
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
#importObjective-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 ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 12
- บทเรียน
- 48
คำถามที่พบบ่อย
บทเรียน “การเชื่อม Objective-C กับ Swift” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การเชื่อม Objective-C กับ Swift” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Objective-C iOS Development for Legacy & Enterprise Apps ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Objective-C iOS Development for Legacy & Enterprise Apps มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การเชื่อม Objective-C กับ Swift”
ทำความเข้าใจวิธีเปิดให้คลาสและเมธอดของ Objective-C ใช้งานจาก Swift ผ่านส่วนหัวสำหรับการเชื่อมภาษา คุณปฏิบัติ Objective-C iOS Development for Legacy & Enterprise Apps ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Objective-C iOS Development for Legacy & Enterprise Apps หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Objective-C iOS Development for Legacy & Enterprise Apps บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 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 ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การเชื่อม Objective-C กับ Swift
- การเชื่อม Swift กับ Objective-C
- การสร้างเฟรมเวิร์ก Objective-C
- การจัดการความเป็นค่าว่างและการจับคู่ชนิดข้อมูลข้ามสะพานเชื่อม