> On Sun, 2009/12/20, Graham Cox <graham....@bigpond.com> wrote: > From: Graham Cox <graham....@bigpond.com> > Subject: Re: passing a method name? > To: "Chunk 1978" <chunk1...@gmail.com> > Cc: "cocoa-dev Dev" <cocoa-dev@lists.apple.com> > Date: Sunday, 2009 December 20, 07:29 >> On 2009/12/21, at 00:18, Chunk 1978 wrote: >> i have a bunch of methods that will call one >> method. currently, i'm passing a string object >> so the called method will know which method >> had called it, and complete the proper task >> based on the method that called it.
> Good programming practice encourages the idea that > functions and methods are complete in and of themselves, and > are invariant under different calling conditions. Variations > should be accomplished using properly passed parameters, not > information about where the function has been called from. > I'd say this approach is going to bite you very, very hard > if you persist with it. Nobody does this in professional > programming, and where it is done inadvertently, it will > usually be rejected and revised when code is peer-reviewed. > So you might consider this comment part of a peer review. > > The only situation this is considered acceptable is when > passing a callback or completion method, where the called > method calls the passed method but no part of its internal > state depends on it. > > > instead of creating strings and passing them, is it > possible to pass > > the method name? > > You can pass a SEL (selector) type but unless it's for the > specific purpose of indicating a callback or completion > method, I would strongly advise against it. OTOH http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ActionMessages/Concepts/TargetsAndActions.html http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/SearchFields/Articles/ConfiguringTargetAction.html - (IBAction)updateFilter:sender http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CocoaFundamentals/CommunicatingWithObjects/CommunicateWithObjects.html - (BOOL)application:(NSApplication *)sender openFile:(NSString *)filename; // NSApplication - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender; // NSApplication An action is the message a control sends to the target or, from the perspective of the target, the method it implements to respond to the action. A control or— as is frequently the case in the Application Kit—a control’s cell stores an action as an instance variable of type SEL. SEL is an Objective-C data type used to specify the signature of a message. An action message must have a simple, distinct signature. The method it invokes returns nothing and has a sole argument of type id. This argument, by convention, is named sender. Here is an example from the NSResponder class, which defines a number of action methods: - (void)capitalizeWord:(id)sender; Action methods declared by Cocoa classes can also have the equivalent signature: - (IBAction) deleteRecord:(id)sender; In this case, IBAction does not designate a data type for a return value; no value is returned. IBAction is a type qualifier that Interface Builder notices during application development to synchronize actions added programmatically with its internal list of action methods defined for a project... http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/EventOverview/EventHandlingBasics/EventHandlingBasics.html >From Interface Builder you can generate a header file and implementation file >for your Xcode project that include a declaration and skeletal implementation, >respectively, for each action method defined for a class. These Interface >Builder–defined methods have a return “value” of IBAction, which acts as a tag >to indicated that the target-action connection is archived in a nib file. You >can also add the declarations and skeletal implementations of action methods >yourself; in this case, the return type is void.) The remaining required part >of the signature is a single parameter typed as id and named (by convention) >sender. Listing 3-1 illustrates a straightforward implementation of an action method that toggles a clock’s AM-PM indicator when a user clicks a button. Listing 3-1 Simple implementation of an action method - (IBAction)toggleAmPm:(id)sender { ... } ... _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com