0

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..

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

11 Comments

So how would I be able to get the class name into the Method from an array??
It's not at all clear what you are trying to do. What does out:andlevels: do? How is it defined? What is insample? What is level?
Almost @Jim, "Aslot" is the objectindex which pulls the class out of the array sends it to the call when user makes selection, Not the whole array of classes. I tried adding a protocol but still was getting errors also objc_getClass(Const char* name) so UTF8String didn't work.
"getting errors" and "didn't work" is not helpful feedback. What errors are you getting?
Thanks for the Help Jim, in .h I added the @protocol Class <NSObject> @end then @interface MainClass:UIViewController<Class>, Error: Use of undeclared identifier 'UTF8String', Error: "Receiver type 'Class*' is not 'id' or interface pointer, consider casting it to 'id', Error: Instance method '-alloc' not found(return type defaults to 'id')..... I overlooked Madhu post he was using if statements and it worked by changing self to the Class. But a lot more coding when adding more Classes.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.