I have this inside a class
[delegate performSelector:@selector(doStuff:) withObject:myObject afterDelay:2.0];
and I am having this error
warning: '-performSelector:withObject:afterDelay:' not found in protocol(s)
I cannot figure out what may be wrong.
any clues?
thanks.
Specifying a delay of 0 does not necessarily cause the selector to be performed immediately. The selector is still queued on the thread’s run loop and performed as soon as possible. This method sets up a timer to perform the aSelector message on the current thread’s run loop.
If you want the message to be dequeued when the run loop is in a mode other than the default mode, use the performSelector:withObject:afterDelay:inModes: method instead.
This method sets up a timer to perform the aSelector message on the current thread’s run loop. The timer is configured to run in the default mode ( NSDefaultRunLoopMode ). When the timer fires, the thread attempts to dequeue the message from the run loop and perform the selector.
Your problem is that you've declared your delegate instance variable as:
id<SomeProtocol> delegate;
Right? Well, that means that the compile-time checker is only going to look for methods in the <SomeProtocol>
protocol. However, performSelector:withObject:afterDelay:
is declared on NSObject
. This means that you should declare the ivar as:
NSObject<SomeProtocol> * delegate;
This is saying that it must be an NSObject
that conforms to <SomeProtocol>
, as opposed to any object that conforms to <SomeProtocol>
. This should get rid of your warning, and you don't have to do any casting.
Try casting the delegate to its class' type first, then invoke performSelector:withObject:afterDelay
:
[(SomeClass *) delegate performSelector:@selector(doStuff:) withObject:myObject afterDelay:2.0];
Assuming that your delegate
is of type id
, you need to tell the runtime that the object in fact does inherit from NSObject
(where the performSelector:withObject:afterDelay:
method is defined) by casting it to it's class' type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With