第一次使用协议 - Objective-C
这是我第一次在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该阅读 Objective 中有关协议的部分-C 再次指南 :) 我认为你并没有真正理解协议是如何工作的。这就是你想要的:
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:
协议本质上是一个契约,例如,“符合
ReportsReceiver
协议的对象必须实现receiveData:
方法”。因此,MyController.h 承诺
receiveData:
将出现,并且 MyController.m 履行了承诺。到目前为止,一切都很好。现在,您的
receiver
变量并不关心接收者到底是什么类型的对象,只要它符合ReportsReceiver
协议即可。您声明的方式是:...在初始化中您可能会说:
然后像这样调用它:
A protocol is, in essence, a contract that says, for example, "an object conforming to the
ReportsReceiver
protocol must implement thereceiveData:
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 theReportsReceiver
protocol. The way you declare that is:...and in your initialization you might say:
Then invoke it like:
首先将 NSObject 协议添加到您自己的协议中。您收到的警告是来自 NSObject 的方法。
当声明一个实现协议的对象时,它应该更像:
或者
在您创建一个实现ReportsReceiver协议的对象(ReceiverClass)的情况下。
您分配一个实现协议的类的方式与分配任何其他类的方式相同:
@protocol 指令开始声明协议,而不是强制转换为协议。查看文档了解具体操作方法使用它们。
Start with adding the NSObject protocol to your own protocol. The warnings you are getting are methods from NSObject.
When declaring an object that implements a protocol, it should be more like:
or
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:
The @protocol directive begins declaring a protocol, not casting to one. Check out the docs for how to use them.