Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iphone - performSelector:withObject:afterDelay:' not found in protocol(s)?

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.

like image 514
Duck Avatar asked Nov 28 '10 02:11

Duck


People also ask

What happens if aselector is delayed?

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.

How to dequeued the message when the run loop is delayed?

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.

How do I use aselector in a run loop?

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.


2 Answers

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.

like image 114
Dave DeLong Avatar answered Nov 15 '22 17:11

Dave DeLong


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.

like image 24
Jacob Relkin Avatar answered Nov 15 '22 17:11

Jacob Relkin