โพรโทคอลและหมวดหมู่
สำรวจโพรโทคอลสำหรับกำหนดข้อตกลงการสื่อสาร และหมวดหมู่สำหรับเพิ่มเมธอดให้คลาสที่มีอยู่โดยไม่ต้องสร้างคลาสย่อย
โพรโทคอลและหมวดหมู่ เป็นบทเรียน Objective-C iOS Development for Legacy & Enterprise Apps ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Objective-C iOS Development for Legacy & Enterprise Apps และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Objective-C iOS Development for Legacy & Enterprise Apps มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Extending Behavior: Intro
Welcome to this lesson on Objective-C Protocols and Categories! These are powerful features that allow you to extend the functionality of your classes in flexible ways.
We'll explore how protocols define contracts for communication between objects and how categories let you add methods to existing classes without needing to subclass them.
Protocols: The Communication Contract
Think of an Objective-C protocol as a blueprint or a contract. It declares a list of methods that other classes can choose to implement.
- A class that 'adopts' a protocol promises to implement its required methods.
- This is crucial for establishing communication patterns, like the popular delegate pattern.
- Protocols are similar to interfaces in languages like Java or C#.
Defining a Protocol
To define a protocol, you use the @protocol directive. You can specify methods as @required (must be implemented) or @optional (can be implemented).
Protocols often conform to <NSObject>, meaning any class adopting it must be an Objective-C object.
@protocol MyServiceDelegate <NSObject>
@required
- (void)serviceDidFinishLoadingData:(NSArray *)data;
@optional
- (void)serviceDidStartRequest;
@endAdopting & Implementing a Protocol
A class adopts a protocol by listing it in angle brackets after its superclass. Then, it must implement all @required methods.
Let's see an example where a DataLoader class needs a delegate to tell it when data is ready.
#import <Foundation/Foundation.h>
// 1. Define the delegate protocol
@protocol DataLoaderDelegate <NSObject>
- (void)dataLoader:(id)loader didFinishLoadingData:(NSString *)data;
- (void)dataLoaderDidFail:(id)loader withError:(NSError *)error;
@end
// 2. Class that uses the delegate
@interface MyDataLoader : NSObject
@property (nonatomic, weak) id<DataLoaderDelegate> delegate; // The delegate property
- (void)loadData;
@end
@implementation MyDataLoader
- (void)loadData {
NSLog(@"DataLoader: Starting data load...");
// Simulate network request
NSString *result = @"Sample Data Loaded!";
// Notify the delegate
if ([self.delegate respondsToSelector:@selector(dataLoader:didFinishLoadingData:)]) {
[self.delegate dataLoader:self didFinishLoadingData:result];
}
}
@end
// 3. Class that conforms to the protocol (the delegate)
@interface MyDataConsumer : NSObject <DataLoaderDelegate> // Adopting the protocol
- (void)startConsumption;
@end
@implementation MyDataConsumer
- (void)startConsumption {
MyDataLoader *loader = [[MyDataLoader alloc] init];
loader.delegate = self; // Set self as the delegate
[loader loadData];
}
#pragma mark - DataLoaderDelegate Methods
- (void)dataLoader:(id)loader didFinishLoadingData:(NSString *)data {
NSLog(@"DataConsumer: Received data: %@", data);
}
- (void)dataLoaderDidFail:(id)loader withError:(NSError *)error {
NSLog(@"DataConsumer: Failed with error: %@", error.localizedDescription);
}
@end
// 4. Main execution
int main(int argc, const char * argv[]) {
@autoreleasepool {
MyDataConsumer *consumer = [[MyDataConsumer alloc] init];
[consumer startConsumption];
}
return 0;
}Categories: Extending Classes
Categories provide a way to add new methods to an existing class, even one you don't own (like NSString or NSObject), without modifying its original source code or creating a subclass.
- They are great for organizing code into logical groups.
- You cannot add new instance variables to a class using a category.
- If a category method has the same name as an existing method, the category's implementation will be used (this can lead to unexpected behavior!).
Defining a Category
To define a category, you create an interface file (.h) and an implementation file (.m). The syntax looks like this:
@interface ClassName (CategoryName)Let's create a category on NSString to add some utility methods.
// NSString+MyStringAdditions.h
#import <Foundation/Foundation.h>
@interface NSString (MyStringAdditions)
- (BOOL)isEmailValid;
- (NSString *)stringByCapitalizingFirstLetter;
@endImplementing Category Methods
Now let's implement the methods we declared in our NSString+MyStringAdditions category. Once implemented, these methods become available on any NSString instance!
Try running this example to see how it works:
#import <Foundation/Foundation.h>
// 1. Define the Category Interface
@interface NSString (MyStringAdditions)
- (NSString *)stringWithPrependedHello;
- (NSString *)stringWithAppendedExclamation;
@end
// 2. Implement the Category Methods
@implementation NSString (MyStringAdditions)
- (NSString *)stringWithPrependedHello {
return [NSString stringWithFormat:@"Hello, %@", self];
}
- (NSString *)stringWithAppendedExclamation {
return [NSString stringWithFormat:@"%@!", self];
}
@end
// 3. Main execution
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSString *name = @"CoddyKit";
// Use category methods
NSString *greeting = [name stringWithPrependedHello];
NSString *excitedGreeting = [greeting stringWithAppendedExclamation];
NSLog(@"Original: %@", name);
NSLog(@"Greeting: %@", greeting);
NSLog(@"Excited: %@", excitedGreeting);
}
return 0;
}When to Use Protocols vs. Categories
It's important to know when to choose between protocols and categories:
- Protocols: Use when you need to define a common interface or contract that multiple unrelated classes might conform to (e.g., delegate pattern, data sources). They define what an object can do.
- Categories: Use when you want to add utility methods to an existing class, group related methods, or extend a class without subclassing (e.g., adding a custom parsing method to
NSString). They extend how an object can behave.
Protocols & Inheritance
A protocol can also adopt other protocols, creating a hierarchy of requirements. For example, @protocol ChildProtocol <ParentProtocol> means ChildProtocol includes all methods from ParentProtocol.
This allows you to build more complex contracts by combining simpler ones.
Quick Check: Protocols
Consider the following Objective-C code snippet:
@protocol DataProcessor
@required
- (void)processData:(NSArray *)data;
@optional
- (void)didStartProcessing;
@end What is the primary purpose of this protocol?
Recap: Protocols & Categories
Great job! In this lesson, you learned about two powerful Objective-C features:
- Protocols: Define a contract of methods that classes can adopt to enable communication and establish common interfaces.
- Categories: Extend existing classes by adding new methods without inheritance, useful for utility functions and code organization.
Mastering these will help you build more flexible and modular Objective-C applications!
คำถามที่พบบ่อย
บทเรียน “โพรโทคอลและหมวดหมู่” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “โพรโทคอลและหมวดหมู่” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Objective-C iOS Development for Legacy & Enterprise Apps ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Objective-C iOS Development for Legacy & Enterprise Apps มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “โพรโทคอลและหมวดหมู่”
สำรวจโพรโทคอลสำหรับกำหนดข้อตกลงการสื่อสาร และหมวดหมู่สำหรับเพิ่มเมธอดให้คลาสที่มีอยู่โดยไม่ต้องสร้างคลาสย่อย คุณปฏิบัติ 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 ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “โพรโทคอลและหมวดหมู่” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Objective-C iOS Development for Legacy & Enterprise Apps นี้ได้ไหม
ได้ บทเรียน Objective-C iOS Development for Legacy & Enterprise Apps ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- คลาส อ็อบเจ็กต์ และเมธอด
- พร็อพเพอร์ตีและตัวแปรของอินสแตนซ์
- โพรโทคอลและหมวดหมู่
- การสืบทอดและการเขียนทับเมธอด