Hi, new to lens C, and wondered if anyone could help me with this. I have several different methods, each of which requires 3 input values ββand usually calls it with
[self methodA:1 height:10 speed:3]
but the name of the method that I want to read from the string in plist, 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]
etc.
Any ideas how I can do this, I tried to define 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, any help would be greatly appreciated. We tried the solution below, but could not work, thatβs what I tried.
So, just to repeat, I have methods like
spawnEnemyA:2 withHeight:3 withSpeed:4 spawnEnemyB:3 withHeight:2 withSpeed:5
and I want to read the values ββthat I want to pass to these methods, as well as the type of method from the plist file. my code is as follows: //////////////////////////////////////////// //////////////////
// These are the values ββI read from 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 errors, but the methods are not called. Any ideas? Greetings