2011-04-09 33 views
0

我使用這個類:如何子類化? DarwiinRemote目標C

 

@interface NSObject(WiiRemoteDiscoveryDelegate) 

- (void) WiiRemoteDiscovered:(WiiRemote*)wiimote; 
- (void) WiiRemoteDiscoveryError:(int)code; 

@end; 
 

但我如何繼承呢?

回答

0

這就是所謂的非正式協議,是怎樣的Cocoa框架主要申報的Mac OS X的最新版本

的代碼塊被簡單地說,它會調用該方法之前委託方法「WiiRemoteDiscovered:」和「WiiRemoteDiscoveryError:」在某個對象上。 「WiiRemoteDiscoveryDelegate」類別的名稱表明它計劃在委託上調用這些方法。

想象一些這樣的代碼:

@interface WiiRemoteDiscoverer { 
    id delegate; 
} 
@property id delegate; 
- (void)startDiscovery; 
@end 

@implementation WiiRemoteDiscoverer 
@synthesize delegate; 
- (void)startDiscovery { 
    /* do the discovery ... */ 
    [delegate WiiRemoteDiscoveryError:-1]; 
} 
@end 

如果你要建立的是,你會得到調用WiiRemoteDiscoveryError行編譯器警告:因爲該方法尚未在任何地方聲明。爲了避免這一點,你可以做兩件事之一。你可以做這個類的作者沒有和它添加到一個標題:

@interface NSObject(WiiRemoteDiscoveryDelegate) 
- (void) WiiRemoteDiscovered:(WiiRemote*)wiimote; 
- (void) WiiRemoteDiscoveryError:(int)code; 
@end; 

該塊基本上是說,每一個對象實現WiiRemoteDiscovered:(這是不正確的),和沉默的編譯器警告。

或者你可以做一些比較正規的是這樣的:

@protocol WiiRemoteDiscovererDelegate <NSObject> 
- (id)wiiRemoteDiscoveryError:(int)errorCode; 
@end 

@interface WiiRemoteDiscoverer { 
    id <WiiRemoteDiscovererDelegate> delegate; 
} 
@property id <WiiRemoteDiscovererDelegate> delegate; 
- (void)startDiscovery; 
@end 

@implementation WiiRemoteDiscoverer 
@synthesize delegate; 
- (void)startDiscovery { 
    /* do the discovery ... */ 
    [delegate wiiRemoteDiscoveryError:-1]; 
} 
@end 
+0

哦,我並不瞭解Objective-C的一切,這樣的感謝。就像你說的那樣,我只是劃分了NSObject。 – Bob 2011-04-10 14:22:04