0Pricing
Objective-C iOS Development for Legacy & Enterprise Apps · บทเรียน

การเชื่อม Swift กับ Objective-C

เรียนรู้การทำให้โค้ด Swift เรียกใช้จาก Objective-C ได้ผ่านส่วนหัว Objective-C ที่สร้างขึ้นโดยอัตโนมัติ

การเชื่อม Swift กับ Objective-C เป็นบทเรียน Objective-C iOS Development for Legacy & Enterprise Apps ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Objective-C iOS Development for Legacy & Enterprise Apps และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Objective-C iOS Development for Legacy & Enterprise Apps มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Swift to Obj-C: The Bridge

Welcome! In mixed-language projects, you often need Swift code to talk to Objective-C code. This lesson focuses on making your modern Swift classes and methods accessible to older Objective-C files.

This "bridging" allows you to gradually introduce Swift into an existing Objective-C codebase or have parts of your app written in different languages.

The Auto-Generated Header

When you add Swift files to an Objective-C project, Xcode automatically generates a special header file for you. It's named ProductModuleName-Swift.h.

This header acts as a translator, exposing your Swift code to the Objective-C world. You don't create it; Xcode does all the work! You just need to import it.

Exposing Swift Classes

For an Objective-C file to "see" a Swift class, that Swift class must inherit from NSObject. This is a fundamental requirement for Objective-C compatibility.

Without inheriting from NSObject, your Swift class won't appear in the auto-generated bridging header.

Using @objc for Visibility

The @objc attribute explicitly marks a Swift declaration (class, method, property, initializer) as available to Objective-C.

  • Classes that inherit from NSObject and are not final automatically expose their members.
  • You might need @objc for specific members if your class doesn't inherit from NSObject (e.g., via a protocol) or for properties/methods that require KVC/KVO compatibility.

A Simple Swift Class

Here's a Swift class, MySwiftGreeter, that inherits from NSObject. Its name property and greet() method will be automatically exposed to Objective-C.

We can optionally use @objc(MySwiftGreeter) to specify the Objective-C name, though it's often inferred.

import Foundation

class MySwiftGreeter: NSObject {
    @objc var name: String

    @objc init(name: String) {
        self.name = name
        super.init()
    }

    @objc func greet() -> String {
        return "Hello from Swift, \(name)!"
    }
}

Importing the Swift Header

To use your Swift classes in an Objective-C file, you simply need to import the auto-generated bridging header. Replace ProductModuleName with your project's actual module name.

This import should be in your Objective-C .m implementation file, not in a .h header file, to avoid circular dependencies and unnecessary exposure.

// In MyObjectiveCFile.m
#import "YourProjectName-Swift.h"

// Now you can use MySwiftGreeter
// ...

Using Swift in Obj-C

Once the bridging header is imported, your Objective-C code can create instances of Swift classes and interact with their exposed properties and methods, just like any other Objective-C object.

Note: This code snippet shows how it would look in a project. Running it directly requires a full Xcode project setup with both Swift and Objective-C files.

#import <Foundation/Foundation.h>
#import "MyProjectName-Swift.h" // Your project's auto-generated header

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // Create an instance of the Swift class
        MySwiftGreeter *greeter = [[MySwiftGreeter alloc] initWithName:@"Coddy"];

        // Call a method on the Swift object
        NSString *greeting = [greeter greet];
        NSLog(@"%@", greeting);

        // Access a property
        NSLog(@"Greeter's name: %@", greeter.name);
    }
    return 0;
}

Swift & Obj-C Type Map

When bridging, Swift types are automatically mapped to their closest Objective-C equivalents:

  • Swift String maps to NSString*
  • Swift Int, Double, Bool map to NSNumber*
  • Swift Array maps to NSArray*
  • Swift Dictionary maps to NSDictionary*

This allows seamless data exchange between the two languages.

Customizing Obj-C Names

Sometimes, Swift's inferred Objective-C name for a class or method might not be ideal, or it might conflict. You can provide a custom Objective-C name using @objc(NewName).

This is especially useful if you need to match existing Objective-C naming conventions or avoid clashes with existing Objective-C symbols.

import Foundation

@objc(LegacyGreeter) // Renames MySwiftGreeter to LegacyGreeter for Obj-C
class MySwiftGreeter: NSObject {
    @objc var name: String

    @objc(initWithPersonName:) // Custom name for initializer
    init(name: String) {
        self.name = name
        super.init()
    }

    @objc(sayHello) // Custom name for method
    func greet() -> String {
        return "Hello from Swift, \(name)!"
    }
}

Bridging Limitations

Not all Swift features can be directly exposed to Objective-C. Keep these limitations in mind:

  • Swift Structs & Enums: Unless they have raw values and conform to @objc, they aren't directly visible.
  • Generics: Swift generics are not exposed.
  • Global Functions: Only methods within NSObject-derived classes are bridged.
  • Tuples: Swift tuples have no Objective-C equivalent.

Focus on classes, methods, and properties that mirror Objective-C's object model.

Bridging Knowledge Check

Which of the following is REQUIRED for a Swift class to be accessible from Objective-C code via the auto-generated bridging header?

Bridging Swift to Obj-C

You've learned how to make your Swift code available to Objective-C! Key takeaways:

  • Xcode generates a ProductModuleName-Swift.h header.
  • Swift classes must inherit from NSObject to be seen by Objective-C.
  • The @objc attribute explicitly exposes members or renames them.
  • Swift types like String, Array, and Dictionary are automatically mapped to their NS counterparts.

This bridging is crucial for maintaining and evolving mixed-language iOS projects!

คำถามที่พบบ่อย

บทเรียน “การเชื่อม Swift กับ Objective-C” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การเชื่อม Swift กับ Objective-C” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Objective-C iOS Development for Legacy & Enterprise Apps ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Objective-C iOS Development for Legacy & Enterprise Apps มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การเชื่อม Swift กับ Objective-C”

เรียนรู้การทำให้โค้ด Swift เรียกใช้จาก Objective-C ได้ผ่านส่วนหัว Objective-C ที่สร้างขึ้นโดยอัตโนมัติ คุณปฏิบัติ 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 ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “การเชื่อม Swift กับ Objective-C” ใช้เวลานานแค่ไหน

บทเรียน 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