views:

159

answers:

1

hello,

I've a String who's value is the name of the Class[MyClass] which has to be instantiated, and MyClass has a method called

 -(void)FunctionInClass;

i'm using the method called NSClassFromString to instantiate MyClass.I want to know

1) what does NSClassFromString return?? 
2) what is id? 
3) How to call method -(void)FunctioninClass which is in MyClass using the instance.

how should i proceed , i'm doing it in Objective-C for iPhone app?

+1  A: 

1) what does NSClassFromString return??

It returns a Class, which means you can do [[NSClassFromString(@"MyClass") alloc] init]; see Mac Dev Center Docs for more info.

2) what is id?

See http://www.otierney.net/objective-c.html#idtype

3) How to call method -(void)FunctioninClass which is in MyClass using the instance.

id myclass = [[NSClassFromString(@"MyClass") alloc] init];
[myclass FunctioninClass];
echo
AFAICT, NSClassFromString is a function, not a class. Point 1 would become NSClassFromString( @"MyClass" )
Martin Cote
Good point. I was going from memory. Still the general idea is there...
echo
@ point 1) if NSClassFromString is returning a class, then that class has to be instantiated n then we can call FunctioninClass method right? but @ point 3) we are directly using the class named myclass and calling function. I tried with point 3) its giving me an error: MyClass undeclared(first use in function) and myclass undeclare (first use in function)
suse
Try it like this: id myclass = [[NSClassFromString(@"MyClass") alloc] init]; If the calling code does not know what MyClass is, you'll get the error you described. By using id as the variable type, you avoid this.
echo
hey.. when i changed to id, the error has gone, and even the control is going to the method when i double click on it, but y it is not printing NSLog(@"hello"); method which is in -(void)FunctioninClass method??
suse
Hey plz reply.. could you make out wher the mistake is.. because there is no error also. then y is it not printing Hello??
suse
can you post some code?
echo
NSString *Class_Name = [subarray objectAtIndex:1]; **// i'll be getting the class name from array...........** id myclass = [[NSClassFromString(@"Class_Name")alloc]init]; [myclass FunctioninClass];
suse
if you set a breakpoint in FunctioninClass, does the breakpoint get hit?
echo
No .:(:(i have set the breakpoint.. but control is not going till there
suse
Control in debugger is not at all getting into this line... [myclass FunctioninClass];
suse
i've this in a for loop,which is many strings to instantiate.
suse
hey.. i got to know tat myclass is nil.. but y is it nil. As per your answer to point 1, you said it returns class name right?
suse
Here's a simple cocoa app that seems to do what you're looking for. I created a new Xcode project called "Untitled". https://pastee.org/ps83z
echo
@echo:Should the name of string [@"MyClass"] be same as name of Class name? i.e in your code,........ @interface MyClass ...........id myclass = [[NSClassFromString(@"MyClass") alloc] init];
suse
hey .. thanks a loooooots :)
suse