Cocoa Selector Question

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

-(void)openBackupNamed:(NSString *)name

using this:

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

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

thank

+3
source share
1 answer

In your case, you will need to create a new NSInvocation object and set it for index parameter 2 to your NSString (parameters with index 0 and 1 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 ADC Link to NSInvocation Class

setArgument. ( ), .

+4

Source: https://habr.com/ru/post/1750834/


All Articles