how to subclass this? DarwiinRemote Objective C
I use this class:
@interface NSObject( WiiRemoteDiscoveryDelegate )
- (void)开发者_如何转开发 WiiRemoteDiscovered:(WiiRemote*)wiimote;
- (void) WiiRemoteDiscoveryError:(int)code;
@end;
but how do I subclass this?
This is called an informal protocol, and is how the Cocoa frameworks primarily declared delegate methods before recent versions of Mac OS X.
This block of code is simply saying that it will invoke the methods "WiiRemoteDiscovered:" and "WiiRemoteDiscoveryError:" on some object. The name of the category "WiiRemoteDiscoveryDelegate" is your indication that it plans to invoke these methods on the delegate.
Imagine some code like this:
@interface WiiRemoteDiscoverer {
id delegate;
}
@property id delegate;
- (void)startDiscovery;
@end
@implementation WiiRemoteDiscoverer
@synthesize delegate;
- (void)startDiscovery {
/* do the discovery ... */
[delegate WiiRemoteDiscoveryError:-1];
}
@end
If you were to build that, you'd get a compiler warning on the line that invokes WiiRemoteDiscoveryError: because that method hasn't been declared anywhere. To avoid that you can do one of two things. You can do what the authors of this class did and add this to a header:
@interface NSObject( WiiRemoteDiscoveryDelegate )
- (void) WiiRemoteDiscovered:(WiiRemote*)wiimote;
- (void) WiiRemoteDiscoveryError:(int)code;
@end;
That block is basically saying that every object implements WiiRemoteDiscovered: (which isn't true), and silence the compiler warning.
Or you could do something more formal like this:
@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
精彩评论