Reputation: 63
What is the use of perform selector in objective C? and can you please tell me the difference between perform selector and responds selector?
Upvotes: 3
Views: 2601
Reputation: 42163
A typical usage of performSelector is to send message to an object when you don't know whether it is valid to do so.
For example, assuming you have a @property (assign) id delegate
in your interface, where id
is a pointer to any kind of object. Even if the delegate
is not nil
, you don't know whether it implements the proper protocol you created for the delegate when compiling your code. So you can:
if ([delegate respondsToSelector:@selector(delegateMethod:)]) {
[delegate performSelector:@selector(delegateMethod:)
withObject:param];
}
This can also be used when you need to detect the type of an object. You can use performSelector:
to avoid type casting. Also this eliminates unnecessary warnings at compile time.
For multi-thread scenarios, you may want to use these methods of NSObject
:
performSelectorOnMainThread:withObject:waitUntilDone:
performSelectorOnMainThread:withObject:waitUntilDone:modes:
performSelector:onThread:withObject:waitUntilDone:
performSelector:onThread:withObject:waitUntilDone:modes:
performSelectorInBackground:withObject:
Reference:
Upvotes: 0
Reputation: 7461
respondsToSelector: Returns a Boolean value that indicates whether the receiver implements or inherits a method that can respond to a specified message.
performSelector: Sends a specified message to the receiver and returns the result of the message.
PerformSelector is used to call method as you want perform that means that you have option to select different option for performing particular task(Method) Example...
– performSelector:withObject:afterDelay: // will execute method after specific delay.. – performSelector:withObject:afterDelay:inModes: – performSelectorOnMainThread:withObject:waitUntilDone: – performSelectorOnMainThread:withObject:waitUntilDone:modes: – performSelector:onThread:withObject:waitUntilDone: – performSelector:onThread:withObject:waitUntilDone:modes:
– performSelectorInBackground:withObject: // Perform task in background. So, Your ManinThread(Application) will not stop responding..like multithreading..
Where direct method will not provide selection for performing task..
For more and detailed explanation visit this reference..
Hope,this will help you...
Upvotes: 0
Reputation: 10776
-respondsToSelector: allows you to test if a certain object responds to a given selector, of if your app would crash if you still sent the message to it.
-performSelector:(withObject:) simply invokes a certain method, e.g.
[object performSelector:@selector(retain)];
would be equal to
[object retain];
Why do you need that?
A handy example is provided by NSArray
: it allows a selector to be performed by all objects it contains, e.g.
[self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
would be equal to
for (UIView *view in self.subviews)
{
[view removeFromSuperview];
}
whereas the -makeObjectsPerformSelector:
is way more elegant.
Upvotes: 5