定义自定义协议
我有一个自定义类。我希望如果任何其他类实例化它,那么它“必须”有一些特定的方法。 如何实现这一目标? 我不想继承它,因为我没有添加任何额外的功能或以任何方式修改其功能。 我想到了自定义协议,但我的类如何知道“只有当定义的协议由实例化它的类实现时,它才应该允许实例化自身。”
场景是classA:基类 classB:基类 classM
具有 base-class
类型的属性。我将其设置为 objclassA 或 objclassB 。 A 类
& classB
实例化 classM
然后 objclassM`` 调用方法
callBack方法,该方法位于
classA&< /代码>B类<代码>。 classM 基类中的警告可能不会响应回调`,
@protocol UITableViewMgrDelegate
@required
-(void)tableRowSelected:(int)idd selectedType:(NSString*)selectedType selectedValue:(NSString*)selectedValue;
@end
@interface UITableViewMgr : UIViewController {
NSMutableArray *dataSo,*IDs;
NSMutableArray *dataSoRight;
UIViewController *backObject;
}
in .m
[backObject tableRowSelected:(NSInteger)[indexPath row] selectedType:[NSString stringWithFormat:@"cell"] selectedValue:[NSString stringWithFormat:@"cell"]];
//warning at this line
// 'UIViewController' may not respond to '-tableRowSelected:selectedType:selectedValue:'
谢谢sssssss,我通过在类中定义自定义协议来摆脱这些警告
@protocol UITableViewMgrDelegate
@required
-(void)tableRowSelected:(int)idd selectedType:(NSString*)selectedType selectedValue:(NSString*)selectedValue;
@optional
- (void)AddList:(NSString*)value isNew:(int)isNew;
@end
I have a custom class.And I want if any other class instantiate it then it "must" have some specific method.
How to achieve this?
I don't want to inherit it cause I m not adding any extra feature or modifying its functionality in any way.
I thought of custom protocol but how will my class know that "it should allow to instantiate itself only if defined protocol is implemented by class being instantiating it."
scenario isclassA : base-class classB : base-class classM
has property of type base-class
. which i set as objclassA
or objclassB
. ClassA
& classB
instantiate classM
then objclassM`` calls method
callBackmethod which is in both
classA&
classB. warning in
classM base-class may not response to callBack`
@protocol UITableViewMgrDelegate
@required
-(void)tableRowSelected:(int)idd selectedType:(NSString*)selectedType selectedValue:(NSString*)selectedValue;
@end
@interface UITableViewMgr : UIViewController {
NSMutableArray *dataSo,*IDs;
NSMutableArray *dataSoRight;
UIViewController *backObject;
}
in .m
[backObject tableRowSelected:(NSInteger)[indexPath row] selectedType:[NSString stringWithFormat:@"cell"] selectedValue:[NSString stringWithFormat:@"cell"]];
//warning at this line
// 'UIViewController' may not respond to '-tableRowSelected:selectedType:selectedValue:'
thankssssssss I got rid off those warnings by defining custom protocol in my class this way
@protocol UITableViewMgrDelegate
@required
-(void)tableRowSelected:(int)idd selectedType:(NSString*)selectedType selectedValue:(NSString*)selectedValue;
@optional
- (void)AddList:(NSString*)value isNew:(int)isNew;
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以检查某个类是否符合给定协议,
请参阅 内省
一个真实的例子。请注意,
delegate
是在id中定义的。 delegate;
,意味着作为委托的对象应该符合协议VSKeypadViewDelegate
You can check if a certain class conforms to a given protocol
see Introspection
A real-word example. Note that the
delegate
is definedid<VSKeypadViewDelegate> delegate;
, what means that an object, that is meant to be the delegate should conform to the protocolVSKeypadViewDelegate
使用委托怎么样?
您可以将实例化您的类的对象设置为其委托。然后,在类的代码中,您可以通过调用respondsToSelector来检查委托是否具有您正在查找的方法
How about using a delegate?
You would set the object that is instantiating your class as its delegate. Then, in your class's code, you could check to see if the delegate has the method you are looking for by calling respondsToSelector
你的头类应该是这样的:
ClassB 应该是这样的:
如果你使用
你必须在 ClassB 中实现该方法,否则你会收到警告。your header class should look like this:
and ClassB something like this:
if you use
<myDelegate>
you have to implement the method in ClassB otherwise you get a warning.将实例变量 backObject 的声明更改为:
如果您收到有关 classA 或 classB 不符合 UITableViewMgrDelegate 的警告,只需将其添加到它们的接口中:
Change your declaration of the instance variable backObject to:
If you get warnings about classA or classB not conforming to UITableViewMgrDelegate, just add it to their interfaces: