suse suse - 5 months ago 31
Objective-C Question

NSClassFromString returns nil

Why does

? As per the definition it has to return class name.
How should I take care to rectify this problem? I need to instantiate a class from string and call the method, which is in the class, using the instance created.

This is how my code looks like:

id myclass = [[NSClassFromString(@"Class_from_String") alloc] init];
[myclass method_from_class];

But the
function is not being called, control is not going into it. And my code is error free. Any idea how to solve this in Objective-C?


The Documentation for the function says:

Return Value The class object named by aClassName, or nil if no class by that name is currently loaded. If aClassName is nil, returns nil.

An example of how this should be properly used is as follows:

Class dictionaryClass = NSClassFromString(@"NSMutableDictionary");
id object = [[dictionaryClass alloc] init];
[object setObject:@"Foo" forKey:@"Bar"];