Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to instantiate an object of class from string in Objective-C?

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?

like image 429
suse Avatar asked Feb 09 '10 03:02

suse


People also ask

How to create class instance in Objective-C?

You can create a class reference with the following code: Class M = [NSMutableString class]; // NSMutableString (for example). You can then call methods on that saved class with code like this: [M string];

How to create NSObject class in Objective-C?

Creating a Custom ClassGo ahead an choose “Objective-C class” and hit Next. For the class name, enter “Person.” Make it a subclass of NSObject. NSObject is the base class for all objects in Apple's developer environment. It provides basic properties and functions for memory management and allocation.

How do I find the class of an object in Objective-C?

[yourObject isKindOfClass:[a class]] // Returns a Boolean value that indicates whether the receiver is an instance of // given class or an instance of any class that inherits from that class.


1 Answers

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]; 
like image 158
echo Avatar answered Sep 29 '22 18:09

echo