如何子类化这个? DarwinRemote Objective C

发布于 2024-10-31 01:15:22 字数 213 浏览 4 评论 0原文

我使用这个类:


@interface NSObject( WiiRemoteDiscoveryDelegate )

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

@end;

但是我如何子类化它?

I use this class:


@interface NSObject( WiiRemoteDiscoveryDelegate )

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

@end;

but how do I subclass this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷弦 2024-11-07 01:15:22

这称为非正式协议,也是 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文