Hi im new to objective C and was wondering if someone might be able to help me with this. I have a few different methods each requiring 3 input values and normally call it using
[self methodA:1 height:10 speed:3]
but the method name I want to read from a string in a plist so for example if the string was methodB i would get
[self methodB:1 height:10 speed:3]
for "methodC"
[self methodC:1 height:10 speed:3]
and so on.
Any ideas how I might do this I tried defining the string as a Selector using NSSelectorFromString
NSString *string = [plistA objectForKey:@"method"];
SEL select = NSSelectorFromString(string);
[self performSelector:select:c height:b speed:a];
However this did not work either any help would be greatly appreciated. Have tried the solution below but could not get to work here is what i've tried.
So just to recap I have methods such as
spawnEnemyA:2 withHeight:3 withSpeed:4
spawnEnemyB:3 withHeight:2 withSpeed:5
and I want to read the values I want to pass to these methods as well as the method type from a plist file. my code is as follows, //////////////////////////////////////////////////////////////
//These are the values I read from the plist that I want my method to use
int a = [[enemySettings objectForKey:@"speed"] intValue];
int b = [[enemySettings objectForKey:@"position"] intValue];
int c = [[enemySettings objectForKey:@"delay"] intValue];
// I Also read the method name from the plist and combine it into a single string
NSString *method = [enemySettings objectForKey:@"enemytype"];
NSString *label1 = @"spawn";
NSString *label2 = @":withHeight:withSpeed:";
NSString *combined = [NSString stringWithFormat:@"%@%@%@",label1, method,label2];
//Check that the string is correct get spawnEnemyA:withHeight:withSpeed:
CCLOG(@"%@",combined);
//This is the Invocation part
NSInvocation * invocation = [ NSInvocation new ];
[ invocation setSelector: NSSelectorFromString(combined)];
[ invocation setArgument: &c atIndex: 2 ];
[ invocation setArgument: &b atIndex: 3 ];
[ invocation setArgument: &a atIndex: 4 ];
[ invocation invokeWithTarget:self ];
[invocation release ];
////////////////////////////////////////////////////////////////////
The code compiles without any errors but the methods are not called. Any ideas? Cheers
objc_msgSend()
for both speed and, most importantly, type safety in that the compiler will complain if I screw up the types later. – Prodrome