我是否需要 @protocol 声明或其他内容来确保子类定义选择器?
我有一个类,它执行一些用于写入数据库的通用代码。调出弹出窗口和控件等。虽然我可以将多种类型的元素写入数据库,但每个子类都需要有自己的 -(void) writeTagValue
选择器来实现元素的写入。
基类有一个选择器来调用 self.writeTagValue
,但由于基类并不真正执行任何写入操作,因此它的 -(void) writeElement
选择器为空其中包含 abort();
。
我已经在基类中实现了一个协议。h
@protocol IoUISEWriteAnimation <NSObject>
-(void) writeTagValue;
-(IBAction)saWriteValue:(NSNotification *)notification;
@end
将该协议添加到了子类中,现在如果子类没有定义选择器,我会收到编译器警告。
我想知道的是,有没有办法删除基类中空的 -(void) writeElement
选择器?
I have a class that does some generic code for Writing to a Database. Bring up the popovers, and controls, etc. Though there are many types of elements that I can write out to the database, each sub-class needs to have its own -(void) writeTagValue
selector to implement the element's write.
The base-class has a selector that does the call to self.writeTagValue
though since the base class does not really do any writing, its -(void) writeElement
selector is empty with an abort();
in it.
I've implemented a protocol in the base class.h
@protocol IoUISEWriteAnimation <NSObject>
-(void) writeTagValue;
-(IBAction)saWriteValue:(NSNotification *)notification;
@end
added the protocol to the Sub-classes and now if the sub-classes don't define the selectors, I get compiler warnings.
What I want to know is, is there a way to remove the empty -(void) writeElement
selector in the base class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你可以这样做:
当你需要调用一个可选方法时,你可以这样做:
You can do something like this:
And when you need to call an optional method, you do something like this:
只要实现类实现了协议中所需的方法,您的基类就不需要该方法的空实现。但是,由于它是必需的,因此基类无法指定它实现该协议。
你走在正确的道路上。
So long as the implementation class implement the required methods in the protocol, your base class does not need an empty implementation of that method. However, since it's required, the base class cannot specify that it implements the protocol.
You're on the right path.