tags:

views:

308

answers:

4

I have been using @selector today for the first time and have not been able to work out how to do the following? How would you write the @selector if you had more than one argument?

NO ARGUMENTS:

-(void)printText {
    NSLog(@"Fish");
}

[self performSelector:@selector(printText) withObject:nil afterDelay:0.25];

SINGLE ARGUMENT:

-(void)printText:(NSString *)myText {
    NSLog(@"Text = %@", myText);
}

[self performSelector:@selector(printText:) withObject:@"Cake" afterDelay:0.25];

DOUBLE ARGUMENTS:

-(void)printText:(NSString *)myText andMore:(NSString *)extraText {
    NSLog(@"Text = %@ and %@", myText, extraText);
}

[self performSelector:@selector(printText:andMore:) withObject:@"Cake" withObject:@"Chips"];

MULTIPLE ARGUMENTS: (i.e. >2)

NSInvocation

gary

+4  A: 
@selector(printText:andMore:)
KennyTM
thank you, but how would I specify the arguments @"Cake" and @"More Cake"?
fuzzygoat
+3  A: 
[self performSelector:@selector(printText:andMore) withObject:@"Cake" withObject:@"More Cake"];
pheelicks
There is `-performSelector:withObject:withObject:` and `-performSelector:withObject:afterDelay:`, but the mix of the two does not exist.
KennyTM
Right you are. I was copy/pasting and made a mistake. Good spot
pheelicks
+6  A: 

 

 - (id)performSelector:(SEL)aSelector
           withObject:(id)anObject  
           withObject:(id)anotherObject

From the Documentation:

This method is the same as performSelector: except that you can supply two arguments for aSelector. aSelector should identify a method that can take two arguments of type id. For methods with other argument types and return values, use NSInvocation.

so in your case you would use:

[self performSelector:@selector(printText:andMore:)
           withObject:@"Cake"
           withObject:@"More Cake"]
cobbal
+2  A: 

As KennyTM pointed out, the selector syntax is

@selector(printText:andMore:)

You call it with

performSelector:withObject:withObject. 

... if you need more arguments or different types, you need to use NSIvocation

VoidPointer