Declare conditional delegate methods in iOS

206 views Asked by At

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. 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

Thanks!

2

There are 2 answers

0
johnpatrickmorgan On BEST ANSWER

Sadly this isn't possible, though you could always coalesce the two delegate methods into one, e.g.,

- (void)delegateMethodWithResult:(id)result error:(NSError *)error 
0
0yeoj On

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.