Re: about @selector
Re: about @selector
- Subject: Re: about @selector
- From: email@hidden
- Date: Thu, 09 May 2002 15:41:39 -0700
Antonio Inojal wrote:
|what does @selector(drawAnother:) do? In specific what is the @selector
|used for?
A "selector" is the name of a method, separate from any class that may use it. In this case, the method is, as you correctly stated, the "drawAnother:" method of the class. "@selector(drawAnother:)" lets you create a constant whose value is the name of the selector, in a form that can be used while your program is running. Its purpose is to let the program decide when it runs--not when it's compiled--what method of an object to call. Most often, it's used to tell some other code what method to call when something needs to be done. For example, various system objects send "notifications" when interesting things happen. When you have an object ask to be sent one of those notifications, you tell the notification center what method to call when the notification is sent. This way, you can have different notifications call different methods, and the notification center doesn't need to know about any of those methods until runtime.
Any object can use selectors this way. NSObject defines a method performSelector: which simply calls the method specified by the selector. You could, for example, write
SEL whatToDo;
if(today().equals(@"Tuesday"))
whatToDo = @selector(doLaundry);
else
whatToDo = @selector(seeMovie);
[self performSelector: whatToDo];
This code is does exactly the same thing as
if(today().equals(@"Tuesday"))
[self doLaundry];
else
[self seeMovie];
The value here is that you could make the decision in one place, save the selector you chose, and then call performSelector: somewhere else, or tell some other object what method to call.
Glen Fisher
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.