Reputation: 159
I'm trying to get a string in my call from an array. But have no luck. It's being passed to another class.
MainClass.h
#import First
@class First;
@interface MainClass : UIViewController{
NSMutableArray *listArray;
}
///////////////////
MainClass.m
First *first = [[First alloc] init];
listArray = [[NSMutableArray alloc]init];
[listArray addObject:@"first"];
[listArray addObject:@"second"];
int Aslot = 0;
int sumA;
float level = 5, insample = 10;
NSString *slotA =[listArray objectAtIndex:ASlot];
sumA = [slotA out:insample andlevels:level];
/////////
First.h
-(float)out:(float)insample andlevels:(float)level;
First.m
-(float)out:(float)insample andlevels:(float)level{
float outsample = insample + 10 * level;
return outsample;
}
I want slotA (the Class) to equal a string from the array "first" or "second", so it can call the method.
I have a table which when I select first, it sends samples and other parameters to another class where it does processing then returns back to MainClass.
sumA = [first out:insample andlevels:level];
But I'm getting an error saying that NSString may not respond to my parameters out:andlevels. Please help..
Upvotes: 0
Views: 301
Reputation: 73936
Edit:
If I understand correctly, you want to dynamically create an instance of a class whose name you have stored as an NSString
. You can do that this way:
int Aslot = 0;
int sumA;
float level = 5, insample = 10;
NSString *className = [listArray objectAtIndex:Aslot];
Class sampler = objc_getClass([className UTF8String]);
id instance = [[sampler alloc] init];
sumA = [instance out:insample andlevels:level];
// Do whatever you want with sumA here.
[instance release];
If you're doing this a lot, you'll probably want to either keep the instance around or store the classes in the array instead of their names, depending on exactly what you are doing.
Upvotes: 1