Objective-C: Calling selectors with multiple arguments Objective-C: Calling selectors with multiple arguments objective-c objective-c

Objective-C: Calling selectors with multiple arguments


In Objective-C, a selector's signature consists of:

  1. The name of the method (in this case it would be 'myTest') (required)
  2. A ':' (colon) following the method name if the method has an input.
  3. A name and ':' for every additional input.

Selectors have no knowledge of:

  1. The input types
  2. The method's return type.

Here's a class implementation where performMethodsViaSelectors method performs the other class methods by way of selectors:

@implementation ClassForSelectors- (void) fooNoInputs {    NSLog(@"Does nothing");}- (void) fooOneIput:(NSString*) first {    NSLog(@"Logs %@", first);}- (void) fooFirstInput:(NSString*) first secondInput:(NSString*) second {    NSLog(@"Logs %@ then %@", first, second);}- (void) performMethodsViaSelectors {    [self performSelector:@selector(fooNoInputs)];    [self performSelector:@selector(fooOneInput:) withObject:@"first"];    [self performSelector:@selector(fooFirstInput:secondInput:) withObject:@"first" withObject:@"second"];}@end

The method you want to create a selector for has a single input, so you would create a selector for it like so:

SEL myTestSelector = @selector(myTest:);


Your method signature is:

- (void) myTest:(NSString *)

withAString happens to be the parameter (the name is misleading, it looks like it is part of the selector's signature).

If you call the function in this manner:

[self performSelector:@selector(myTest:) withObject:myString];

It will work.

But, as the other posters have suggested, you may want to rename the method:

- (void)myTestWithAString:(NSString*)aString;

And call:

[self performSelector:@selector(myTestWithAString:) withObject:myString];


@Shane Arney

performSelector:withObject:withObject:

You might also want to mention that this method is only for passing maximum 2 arguments, and it cannot be delayed. (such as performSelector:withObject:afterDelay:).

kinda weird that apple only supports 2 objects to be send and didnt make it more generic.