第一次使用协议 - Objective-C

发布于 2024-10-10 13:17:36 字数 1232 浏览 2 评论 0原文

这是我第一次在 Objective-C 中使用协议,我遇到了麻烦: 这是我得到的:

我有一个 ReportsReceiver.h:

@protocol ReportsReceiver


-(void)receiveData:(NSArray *)theData;

@end

我有一个 MyController.h:

@interface MyController : UIViewController<ReportsReceiver,UITableViewDelegate,UITableViewDataSource> {

}
@end

我有一个 MyController.m,其中包含实现的方法:

- (void)receiveData:(NSArray *)theData {
    NSLog(@"received some data!");
}

然后我有一个 AllUtilities.m 类,其声明:

Protocol *receiverProtocol;

AllUtilities.m 还包含一个初始化协议的方法:

- (void)initProtocol {
    receiverProtocol = @protocol(ReportsReceiver);

}

然后稍后在 AllUtilities.m 中我进行调用:

[receiverProtocol receiveData:anArray];

这使应用程序崩溃并出现错误:

2011-01-07 11:46:27.503 TestGA[91156:207] *** NSInvocation: warning: object 0x9c28c of class 'Protocol' does not implement methodSignatureForSelector: -- trouble ahead
2011-01-07 11:46:27.504 TestGA[91156:207] *** NSInvocation: warning: object 0x9c28c of class 'Protocol' does not implement doesNotRecognizeSelector: -- abort

如何我可以解决这个问题吗?谢谢!!

This is my first time using Protocols in Objective-C, and I'm running into a trouble: Here's what I've got:

I have a ReportsReceiver.h:

@protocol ReportsReceiver


-(void)receiveData:(NSArray *)theData;

@end

I have a MyController.h:

@interface MyController : UIViewController<ReportsReceiver,UITableViewDelegate,UITableViewDataSource> {

}
@end

I have a MyController.m with the implemented method:

- (void)receiveData:(NSArray *)theData {
    NSLog(@"received some data!");
}

And then I have a class AllUtilities.m with the declaration:

Protocol *receiverProtocol;

AllUtilities.m also contains a method to initialize the protocol:

- (void)initProtocol {
    receiverProtocol = @protocol(ReportsReceiver);

}

And then later on in AllUtilities.m I make the call:

[receiverProtocol receiveData:anArray];

Which crashes the application with the error:

2011-01-07 11:46:27.503 TestGA[91156:207] *** NSInvocation: warning: object 0x9c28c of class 'Protocol' does not implement methodSignatureForSelector: -- trouble ahead
2011-01-07 11:46:27.504 TestGA[91156:207] *** NSInvocation: warning: object 0x9c28c of class 'Protocol' does not implement doesNotRecognizeSelector: -- abort

How can I fix this? Thanks!!

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

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

发布评论

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

评论(3

陌路终见情 2024-10-17 13:17:36

您应该阅读 Objective 中有关协议的部分-C 再次指南 :) 我认为你并没有真正理解协议是如何工作的。这就是你想要的:

// DataProducer.h
@protocol DataConsumer
- (void) handleData: (NSArray*) data;
@end

@interface DataProducer
@end

// DataProducer.m
@implementation DataProducer

- (void) generateDataAndPassTo: (id <DataConsumer>) consumer
{
     NSArray *data = …;
     [consumer handleData:data];
}

// SomeController.h
#import "DataProducer.h"

@interface SomeController <DataConsumer>
@end

// SomeController.m
@implementation SomeController

- (void) requestData
{
    // The producer is of type DataProducer.
    // Where you get it is irrelevant here.
    [producer generateDataAndPassTo:self];
}

- (void) handleData: (NSArray*) data
{
    NSLog(@"Got data.");
}

@end

You should read the part about protocols in the Objective-C guide once more :) I think you don’t really understand how protocols work. This is what you want:

// DataProducer.h
@protocol DataConsumer
- (void) handleData: (NSArray*) data;
@end

@interface DataProducer
@end

// DataProducer.m
@implementation DataProducer

- (void) generateDataAndPassTo: (id <DataConsumer>) consumer
{
     NSArray *data = …;
     [consumer handleData:data];
}

// SomeController.h
#import "DataProducer.h"

@interface SomeController <DataConsumer>
@end

// SomeController.m
@implementation SomeController

- (void) requestData
{
    // The producer is of type DataProducer.
    // Where you get it is irrelevant here.
    [producer generateDataAndPassTo:self];
}

- (void) handleData: (NSArray*) data
{
    NSLog(@"Got data.");
}

@end
一个人练习一个人 2024-10-17 13:17:36

协议本质上是一个契约,例如,“符合 ReportsReceiver 协议的对象必须实现 receiveData: 方法”。

因此,MyController.h 承诺 receiveData: 将出现,并且 MyController.m 履行了承诺。到目前为止,一切都很好。

现在,您的 receiver 变量并不关心接收者到底是什么类型的对象,只要它符合 ReportsReceiver 协议即可。您声明的方式是:

id<ReportsReceiver> receiver;

...在初始化中您可能会说:

receiver = myController;

然后像这样调用它:

[receiver receiveData:anArray];

A protocol is, in essence, a contract that says, for example, "an object conforming to the ReportsReceiver protocol must implement the receiveData: method".

So, MyController.h promises that receiveData: will be present, and MyController.m fulfills the promise. So far so good.

Now, your receiver variable doesn't care exactly what type of object the receiver is, so long as it conforms to the ReportsReceiver protocol. The way you declare that is:

id<ReportsReceiver> receiver;

...and in your initialization you might say:

receiver = myController;

Then invoke it like:

[receiver receiveData:anArray];
蓝颜夕 2024-10-17 13:17:36

首先将 NSObject 协议添加到您自己的协议中。您收到的警告是来自 NSObject 的方法。

@protocol ReportsReceiver <NSObject>

-(void)receiveData:(NSArray *)theData;

@end

当声明一个实现协议的对象时,它应该更像:

id<ReportsReceiver> receiverProtocol;

或者

ReceiverClass<ReportsReceiver> *receiverProtocol;

在您创建一个实现ReportsReceiver协议的对象(ReceiverClass)的情况下。

您分配一个实现协议的类的方式与分配任何其他类的方式相同:

ReceiverClass<ReportsReceiver> *receiverProtocol;
- (void)initProtocol {
    receiverProtocol = [[ReceiverClass alloc]init];
}

@protocol 指令开始声明协议,而不是强制转换为协议。查看文档了解具体操作方法使用它们。

Start with adding the NSObject protocol to your own protocol. The warnings you are getting are methods from NSObject.

@protocol ReportsReceiver <NSObject>

-(void)receiveData:(NSArray *)theData;

@end

When declaring an object that implements a protocol, it should be more like:

id<ReportsReceiver> receiverProtocol;

or

ReceiverClass<ReportsReceiver> *receiverProtocol;

in the case that you create an object (ReceiverClass) that implements the ReportsReceiver protocol.

You assign a class that implements a protocol in the same way you assign any other class:

ReceiverClass<ReportsReceiver> *receiverProtocol;
- (void)initProtocol {
    receiverProtocol = [[ReceiverClass alloc]init];
}

The @protocol directive begins declaring a protocol, not casting to one. Check out the docs for how to use them.

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