Objective-C 協議類似 Java 介面,但無父類別、無法定義實例變數。它宣告一組方法供其他類別實作,適用於:
@required:預設值,方法必須實作。@optional:方法可選擇性實作。// OneProtocol.h #import <Foundation/Foundation.h> @protocol OneProtocol <NSObject> @optional - (void)optionalMethod1; - (void)optionalMethod2; @required - (void)requiredMethod1; @end // Test.h #import "OneProtocol.h" @interface Test : NSObject <OneProtocol> @end
// Test.m
#import "Test.h"
@implementation Test
- (void)optionalMethod1 { NSLog(@"可選方法1"); }
- (void)optionalMethod2 { NSLog(@"可選方法2"); }
- (void)requiredMethod1 { NSLog(@"必須實作方法"); }
@end
Test *test = [[Test alloc] init]; [test optionalMethod1]; [test optionalMethod2]; [test requiredMethod1]; [test autorelease];

善用 @optional 可減少強制實作負擔,配合 respondsToSelector: 執行前檢查,提升彈性。