28

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?

suse
  • 10,503
  • 23
  • 79
  • 113

2 Answers2

59

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
  • 7,737
  • 2
  • 35
  • 33
  • AFAICT, NSClassFromString is a function, not a class. Point 1 would become NSClassFromString( @"MyClass" ) – Martin Cote Feb 09 '10 at 03:59
  • @ 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 Feb 09 '10 at 04:03
  • 1
    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 Feb 09 '10 at 04:10
  • 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 Feb 09 '10 at 04:14
  • 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 Feb 09 '10 at 04:24
  • 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 Feb 09 '10 at 04:35
  • 1
    if you set a breakpoint in FunctioninClass, does the breakpoint get hit? – echo Feb 09 '10 at 04:36
  • No .:(:(i have set the breakpoint.. but control is not going till there – suse Feb 09 '10 at 04:41
  • Control in debugger is not at all getting into this line... [myclass FunctioninClass]; – suse Feb 09 '10 at 04:44
  • i've this in a for loop,which is many strings to instantiate. – suse Feb 09 '10 at 04:54
  • 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 Feb 09 '10 at 05:29
  • 1
    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 Feb 09 '10 at 05:55
  • @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 Feb 09 '10 at 09:43
0

NSClassFromString() is a function that returns an Objective-C class. That is, in Objective-C a class is an honourable object. An instance of the "Class" class...

And what is an Objective-C class good for?

for instantiating objects (via alloc init ) for introspection and reflection - to query it for the methods and properties instances of that class are exposed to the user for archiving/de-archiving instances of that class, and... to be able to run class methods (methods that do not require any specific instance of that class.

your last lines actually create an instance of the class whose name is the string passed to NSClassFromString.

Motti Shneor
  • 2,095
  • 1
  • 18
  • 24