您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發基礎

用 protocol 與 NSProxy 在 Objective-C 中模擬多繼承

編輯:iOS 開發基礎

Objective-C 裡一個類只能有一個父類,這就是單一繼承。但我們可以借助protocol 與 NSProxy 來模擬多繼承的效果。

先說 protocol

protocol 最常被拿來做的就是代理——嚴格說它叫「委託」,這裡不多糾結,相信大家都已經很熟了。

關鍵在於:一個類可以同時 conform 多個 protocol,把裡面的方法實作完就好,就這麼簡單。

舉例來說,我有兩個 protocol,分別叫 YFPerson 與 YFChild:

@protocol YFPerson
@required
@property (nonatomic, copy, readonly) NSString *name;
- (NSInteger)age;
- (void)eat;
- (void)sleep;
@optional
- (void)play;
- (void)setName:(NSString *)newName;
@end
@protocol YFChild
@required
- (NSString *)nickname;
- (void)introduceMyselfWithName:(NSString *)name nickname:(NSString *)nickname age:(NSInteger)age;
@optional
- (void)study;
@end

那麼在一個全新的 YFStudent 類裡,只要同時 conform 這兩個 protocol 並實作其中的方法,就等於在一個類裡拿到了多組能力的定義。

YFStudent.m

- (NSString *)nickname {
    return @"龍兒";
}

- (NSInteger)age {
    return 19;
}

- (void)sleep {
    NSLog(@"sleep");
}

- (void)eat {
    NSLog(@"eat");
}

- (void)introduceMyselfWithName:(NSString *)name nickname:(NSString *)nickname age:(NSInteger)age {
    NSLog(@"我叫%@,小名%@,今天%@歲了", name, nickname, @(age));
}

這樣一來,在控制器的 viewDidLoad 裡建立 YFStudent 物件,就能呼叫這些協議中定義的方法:

- (void)viewDidLoad {
    [super viewDidLoad];
    YFStudent *student = [[YFStudent alloc] init];
    student.name = @"小龍女";
    [student eat];
    [student sleep];
    [student introduceMyselfWithName:student.name nickname:student.nickname age:student.age];
}

執行後可以正確輸出結果。

再說 NSProxy

這才是真正的「代理」,不信可以翻翻字典。

NSProxy 這個類和 NSObject 是平起平坐的,而且它沒有 init 方法——也就是說,它只能開闢一塊記憶體空間,而無法完成初始化。那要怎麼讓它「變成」任意一個類呢?大致步驟如下:

  • 宣告一個 YFProxy 類,繼承自 NSProxy
  • 為 YFProxy 加上一個 NSObject 類型的屬性
  • 自訂一個方法,等於替這個屬性賦值
  • 透過這個屬性取得被呼叫方法的 signature
  • 為這次呼叫設定真正的接收者
  • 執行呼叫

逐步來看。第一步,對外暴露一個「變身」方法:

@interface YFProxy : NSProxy
- (id)transformToObject:(NSObject *)object;
@end

第二步,加上一個 NSObject 屬性:

@interface YFProxy ()
@property (nonatomic, strong) NSObject *object;
@end

第三步,實作變身方法:

- (id)transformToObject:(NSObject *)object {
    self.object = object;
    return self.object;
}

第四步,重寫 - (NSMethodSignature *)methodSignatureForSelector:(SEL)sel,取得方法簽名:

- (NSMethodSignature *)methodSignatureForSelector:(SEL)sel {
    NSMethodSignature *methodSignature;
    if (self.object) {
        methodSignature = [self.object methodSignatureForSelector:sel];
    } else {
        methodSignature = [super methodSignatureForSelector:sel];
    }
    return methodSignature;
}

第五步,重寫 - (void)forwardInvocation:(NSInvocation *)invocation,把訊息真正轉發給實作該方法的物件:

- (void)forwardInvocation:(NSInvocation *)invocation {
    if (self.object) {
        [invocation setTarget:self.object];
        [invocation invoke];
    }
}

開始變身

假設現在有兩個類。YFPerson:

@interface YFPerson ()
@property (nonatomic, copy) NSString *name;
@end

@implementation YFPerson
- (void)eat {
    NSLog(@"%@正在吃飯", self.name);
}
@end

YFStudent:

@interface YFStudent ()
@property (nonatomic, copy) NSString *studentNum;
@end

@implementation YFStudent
- (void)study {
    NSLog(@"哥正在學習");
}
@end

那要怎麼用 YFProxy 去「繼承」這兩個類?先建立兩個純淨的物件:

YFPerson *person = [[YFPerson alloc] init];
YFStudent *student = [[YFStudent alloc] init];

接著為 YFProxy 開闢記憶體空間(注意這裡是 alloc,不呼叫 init):

YFProxy *proxy = [YFProxy alloc];

然後變身:

[proxy transformToObject:person];

此時就能自由呼叫 YFPerson 的方法了,甚至是那些宣告在 .m 裡的私有方法也照樣叫得到(雖然編譯器會給警告):

[proxy performSelector:@selector(setName:) withObject:@"小明"];
[proxy performSelector:@selector(eat)];

再變一次身:

[proxy transformToObject:student];

於是又能呼叫 YFStudent 的方法:

[proxy performSelector:@selector(study)];

是不是很神奇?當然,這只是初步探討,基於 Objective-C runtime 還有大量值得挖掘的玩法。

以上就是兩種模擬多繼承的做法。特別是第二種:NSProxy 可以變成任意類別,呼叫任意類的方法,並使用它的屬性。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved