NSClassFromString returns nil
Solution 1
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"];
Solution 2
If you are trying to instantiate a class from a static library, you must add the "-ObjC" flag to the "Other Linker Flags" build setting.
Solution 3
This happened to me when I add an external file to the Xcode project. Adding the .m file to Build Phases > Compile Sources solve the problem.
Solution 4
It is possible that your class is not getting linked if this is the only reference to it.
I had a factory method to instantiate various types of subclass. The factory had a switch statement that went to the appropriate subclass and alloc'd and init'ed it. I noticed that all of the alloc/init statements were exactly the same, except for the name of the class. So I was able to eliminate the entire switch block using the NSClassFromString() function.
I ran into the same problem - the return was nil. This was because the class was not used elsewhere in the program, so it wasn't getting linked, so it could not be found at runtime.
You can solve this by including the following statement:
[MyClass class];
That defeats the whole purpose of what I was trying to accomplish, but it might be all you need.
Solution 5
You also need to make sure the class you are trying to instantiate is included in the project. If you added it later, you made need to click the checkbox next to the Target you are building.
Related videos on Youtube
Comments
-
suse over 3 years
Why does
NSClassFromString
returnnil
? 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
method_from_class
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?-
Brandon Bodnar about 14 years
-
Stefan about 14 yearsMy code is error-free, that's a nice one. :)
-
suse about 14 years:) :) but without serving my purpose :(
-
-
suse about 14 yearsBut the value in the string @"Class_from_String" has the value of the class name. because i've printed it just above ,before instantiating the class from string.Then how come it is taking as nil?
-
suse about 14 yearshey 1've written 3rd line as: [object TestMethod]; TestMethod is the method in same class, but stil the control is not going into the method defination.
-
suse about 14 yearswat should i do. plz tel me some alternative.
-
suse about 14 yearshi Dave.. do u have any idea, for alternative methos for NSClassFromString in Objective-C?My main purpose is to create an instance of class from string and to call a method using the instance created.
-
Stefan about 14 years@suse: Either your string isn't getting loaded correctly or there is no class with that name known to the runtime, in which case you have to load it first.
-
NeilJiang over 12 yearsadd this flag to the static lib or to the main program which uses the static lib?
-
zerodog almost 12 yearsWorked for me setting it in the main program.
-
SpacyRicochet over 11 years@GeorgSchölly What could cause a class not to be loaded to the runtime? I'm having the same issue.
-
Stefan over 11 years@SpacyRicochet: Open a new question where you describe why you think your class should get loaded. Then we might be able to figure out why it doesn't work.
-
newacct over 11 years"By the way, is method_from_class an instance method or a class method ?" Well, if it wasn't an instance method, his code should crash.
-
matm about 11 years@Dave: In my opinion, when no class can be loaded with a given name, return value should be
Nil
((Class)0
- literal null value for classes), notnil
((id)0
- literal null value for objects). Is the documentation not precise here or am I missing something? -
Dave DeLong about 11 years@delirus they're the same thing, so it doesn't really matter.
Nil
may be more semantically correct, but in the end it's all just0
. -
matm about 11 years@Dave: semantical correctness was my only concern. Thanks for feedback.
-
defactodeity over 10 yearsYou are awesome! Saved my life! :) Thank you very much.
-
David Manpearl over 10 yearsThis answer worked for me. I added the new class while a different target was selected. I had to select: Target > Build Phases > Compile Sources > '+' to get the class really added to the project.
-
SpacyRicochet about 10 yearsInterestingly enough, I came upon this post again due to another issue. And again, @GeorgSchölly's comment helped! Add
-ObjC
to the linker flags did the trick. -
Harrison Xi about 7 yearsThis also happened to me when I added .m file but forgot to write @implementation