• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Passing selectors as Arguments (Was: - (void)sortUsingSelector:(SEL)comparator )
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Passing selectors as Arguments (Was: - (void)sortUsingSelector:(SEL)comparator )


  • Subject: Re: Passing selectors as Arguments (Was: - (void)sortUsingSelector:(SEL)comparator )
  • From: Marcel Weiher <email@hidden>
  • Date: Wed, 25 Jul 2001 23:41:17 +0200

On Wednesday, July 25, 2001, at 09:07 Uhr, David P. Henderson wrote:

On Wednesday, July 25, 2001, at 01:58 , Dennis C. De Mars wrote:

You always have to use it [@selector] when you
are passing selectors as arguments.

The last line of this statement is not completely true; you can also do
the following:

SEL aSel = @selector(doSomething); // Uses the @selector directive
SEL anotherSel = NSSelectorFromString(@"doSomethingElse"); // Makes a
selector from a string

Which allows you to pass the variables without using the @selector
directive, but using the directive is often the simplest, easiest way to
pass a method as an argument. The above are more useful if you need to
use the selector more than a few times, and I'll leave it for the
uninitiated to discover how truly useful it can be to have a function
like NSSelectorFromString().

...and then there's the HOM-way, where you pass not just the selector but an entire message expression, as in

[[someArray do] doSomething]

instead of

[someArray makeObjectsPerformSelector:@selector(doSomething)].

Apart from the syntax that's both more readabe and more compact, two differences are that an ordinary message expression can be used and that the number and kinds of arguments do not have to be hardwired. An example of this would be

fullPaths = [[@"Prefix" collect] stringByAppendingString:[pathArray each]];

which pre-pends @"Prefix" to each of the strings in 'pathArray'.

The original sorting example could look something like this:

[[anArray sortUsingMessage] compare:nil];

instead of

[[anArray sortUsingSelector:@selector(compare:)];

although I prefer not to use it in this case because the dummy argument that's required to complete the message expression doesn't really make sense.

Marcel


  • Follow-Ups:
    • Re: Passing selectors as Arguments (Was: - (void)sortUsingSelector:(SEL)comparator )
      • From: "John C. Randolph" <email@hidden>
References: 
 >Passing selectors as Arguments (Was: - (void)sortUsingSelector:(SEL)comparator ) (From: "David P. Henderson" <email@hidden>)

  • Prev by Date: Re: Objective-C vs. Java
  • Next by Date: Objective-C vs. C# was Re: Objective-C vs. Java
  • Previous by thread: Re: Passing selectors as Arguments (Was: - (void)sortUsingSelector:(SEL)comparator )
  • Next by thread: Re: Passing selectors as Arguments (Was: - (void)sortUsingSelector:(SEL)comparator )
  • Index(es):
    • Date
    • Thread