[英]Declare conditional delegate methods in iOS
I am looking for a way to declare the implementation of delegate methods to be conditionally linked to each other. 我正在寻找一种方法来声明委托方法的实现有条件地彼此关联。 I am aware of the way of @required
and @optional
for mark specific methods. 我知道标记特定方法的@required
和@optional
方式。 But I'd like to have a way to mark a method as required, if another is implemented. 但是,如果要实现另一个方法,我想有一种方法可以根据需要标记方法。 Is this possible? 这可能吗?
What I like to do is something like this: 我喜欢做的是这样的:
Think of the following delegate methods: 考虑以下委托方法:
- (void) firstSuccessDelegateMethod;
- (void) firstErrorDelegateMethod;
- (void) secondSuccessDelegateMethod;
- (void) secondErrorDelegateMethod;
Is there a way to declare something like 有没有办法声明类似
if firstSuccessDelegateMethod
is implemented, firstErrorDelegateMethod
is required if secondSuccessDelegateMethod
is implemented, secondErrorDelegateMethod
is required 如果firstSuccessDelegateMethod
实现, firstErrorDelegateMethod
是,如果需要secondSuccessDelegateMethod
实现, secondErrorDelegateMethod
需要
Thanks! 谢谢!
遗憾的是这是不可能的,尽管您总是可以将两个委托方法合并为一个,例如,
- (void)delegateMethodWithResult:(id)result error:(NSError *)error
That is impossible requiring delegates in runtime, but it is possible to dynamically add/implement method in runtime.. check that here 那是不可能的,需要在运行时的代表,但可以动态地添加/实施运行方法.. 检查这里
So, the only possible option is to require firstSuccessDelegateMethod
and add firstErrorDelegateMethod
to the target class when triggered, but the easiest way is to combine the two like what @johnpatrickmorgan said. 因此,唯一可能的选择是要求firstSuccessDelegateMethod
并在触发时将firstErrorDelegateMethod
添加到目标类,但是最简单的方法是像@johnpatrickmorgan所说的那样将两者结合起来。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.