Reputation: 1632
So basically I'm implementing the typical way to handle JavaScript calls in objc using window.location="myobj:mymethod:myarg:myotherarg", however, I'm wondering if there is a way to apply an array of arguments to a method, similar to how you can in JavaScript.
Typically I've been doing
-(void) mymethod:(NSArray*) arr{
//method knows how many arguments it takes and what they mean at each index
}
I'd prefer to do:
-(void) mymethod:(NSString*) myarg myOtherArg: (NSString*) myotherarg{
//do stuff
}
and have a method like this:
+(void) callMethod:(NSString*)selectorName withArgs: (NSArray*)args onObject:(id) obj{
//implementation
}
[JEHelpers callMethod:selector withArgs:someArrayOfArgs onObject:myapp]
is this possible?
Upvotes: 5
Views: 127
Reputation: 52565
It's a bit clumsy but it is possible:
- (id)method:(NSString *)arg1, ... {
va_list args;
va_start(args, arg1);
for (NSString *a = arg1; a!= nil; a= va_arg(args, NSString*)) {
// do stuff
}
}
Conventionally arg1
would be a string that defines the data types and number of extra parameters.
You'd call it like this:
[self method:@"5", @"4", @"3", @"2", @"1"];
Your array method is probably clearer for most purposes.
Upvotes: 0
Reputation: 233
You might want to look into NSInvocation. Specifically, you want to build an NSInvocation with selectorName's method signature (NSObject -methodSignatureForSelector:), then set the selector, target, and arguments. Keep in mind: you should check for the argument types (NSMethodSignature -getArgumentTypeAtIndex:, is it equal to '@'), and the arguments you are passing are indexed starting with 2 (because 0 and 1 are the object's self and the method selector).
I'm not saying this is a good idea, btw; there are very few cases where this approach is warranted.
Upvotes: 1
Reputation: 39915
If you know that no method will take more than two arguments, you could use performSelector:withObject:withObject:
to do these calls. If the method takes less than two arguments, the unused withObject:
fields will be ignored.
+ (id)callMethod:(NSString *)selectorName withArgs:(NSArray *)args onObject:(id)obj {
id arg1 = nil, arg2 = nil;
if([args count]) {
arg1 = [args objectAtIndex:0];
if([args count] > 1])
arg2 = [args objectAtIndex:1];
}
return [obj performSelector:NSSelectorFromString(selectorName)
withObject:arg1 withObject:arg2];
}
If there could be more than two arguments, you will have to use NSInvocation
. This class lets you construct a message by passing the various arguments and defining the selector and object, then send the message and get the result.
+ (id)callMethod:(NSString *)selectorName withArgs:(NSArray *)args onObject:(id)obj {
SEL sel = NSSelectorFromString(selectorName);
NSMethodSignature *signature = [obj methodSignatureForSelector:sel];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setSelector:sel];
[invocation setTarget:obj];
NSUInteger index = 2;
for(id arg in args) {
[invocation setArgument:&arg atIndex:index];
++index;
}
id result;
[invocation setReturnValue:&result];
[invocation invoke];
return result;
}
Upvotes: 2
Reputation: 21383
I'm not sure I entirely understand the question (I don't know JavaScript well). However, you can use NSInvocation to send arbitrary messages to any object. Something like this:
+(void) callMethod:(NSString*)selectorName withArgs: (NSArray*)args onObject:(id) obj
{
SEL selector = NSSelectorFromString(selectorName);
if (![obj respondsToSelector:selector]) {
// Object doesn't respond to selector, so do something else/handle the error
}
NSMethodSignature *methodSignature = [obj methodSignatureForSelector:selector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];
[invocation setTarget:obj];
[invocation setSelector:selector];
for (NSUInteger i=0; i<[args count]; i++) {
id argument = [args objectAtIndex:i];
[invocation setArgument:&argument atIndex:i+2]; // Arg 0 is self, arg 1 is _cmd
}
id result;
[invocation setReturnValue:&result];
[invocation invoke];
}
Upvotes: 2