Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocoa Selector Question

I have a question on how to use a selector with multiple parameters. I need to toggle this:

-(void)openBackupNamed:(NSString *)name

using this:

[backupList addItemWithTitle:file action:@selector(openBackupNamed:) keyEquivalent:@""];

I know that there is the withObject: parameter for these cases, but I can't do this in the addItemWithTitle:action:keyEquivalent: method or am I missing something?

Thanks

like image 563
Pripyat Avatar asked Feb 27 '26 10:02

Pripyat


1 Answers

In your case you will have to create a new NSInvocation object and set it's index 2 parameter to your NSString (The 0- and 1-indexed parameters are reserved).

Example:

// Assuming:
NSString *myString = ...;

/* / */

NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:@selector(openBackupNamed:)]];
[invocation setSelector:@selector(openBackupNamed:)];
[invocation setTarget:self];
[invocation setArgument:&myString atIndex: 2];

[invocation invoke]; // or use invokeWithTarget: instead of the above setTarget method.

Read the ADC NSInvocation Class Reference

Please mind the setArgument message. You have to pass it the address of your parameter (your string), not the actual object itself.

like image 115
Denis 'Alpheus' Cahuk Avatar answered Mar 02 '26 14:03

Denis 'Alpheus' Cahuk