objective-c NSClassFromString 返回 nil

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2227085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 22:54:34  来源:igfitidea点击:

NSClassFromString returns nil

objective-cclassinstance

提问by suse

Why does NSClassFromStringreturn nil? 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.

为什么NSClassFromString返回nil?根据定义,它必须返回类名。我应该如何注意纠正这个问题?我需要从字符串实例化一个类并使用创建的实例调用类中的方法。

This is how my code looks like:

这是我的代码的样子:

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

But the method_from_classfunction 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?

但是method_from_class函数没有被调用,控制没有进入它。我的代码没有错误。知道如何在 Objective-C 中解决这个问题吗?

回答by m.kocikowski

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.

如果您尝试从静态库实例化类,则必须将“-ObjC”标志添加到“其他链接器标志”构建设置。

回答by Dave DeLong

The Documentation for the function says:

该函数的文档说

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

返回值由 aClassName 命名的类对象,如果当前没有加载该名称的类,则为 nil。如果 aClassName 为 nil,则返回 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"];

回答by Al Smith

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.

我有一个工厂方法来实例化各种类型的子类。工厂有一个 switch 语句,该语句转到适当的子类并分配和初始化它。我注意到所有的 alloc/init 语句都完全相同,除了类的名称。所以我能够使用 NSClassFromString() 函数消除整个 switch 块。

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.

这违背了我试图完成的整个目的,但这可能就是你所需要的。

回答by Quang V?nh Hà

This happened to me when I add an external file to the Xcode project. Adding the .m file to Build Phases> Compile Sourcessolve the problem.

当我将外部文件添加到 Xcode 项目时,这发生在我身上。将 .m 文件添加到Build Phases> Compile Sources 可以解决问题。

回答by Boojie Waldron

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.

您还需要确保您尝试实例化的类包含在项目中。如果您稍后添加它,则需要单击您正在构建的目标旁边的复选框。

回答by Laurent Etiemble

Why not decomposing all these calls ? This way, you can check the values between the calls:

为什么不分解所有这些调用?这样,您可以检查调用之间的值:

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

By the way, is method_from_classan instance method or a class method ? If it is the later, then you can directly call method_from_classon the myclassvalue:

顺便说一句,是method_from_class实例方法还是类方法?如果是后者,则可以直接调用method_from_classmyclass值:

[myclass method_from_class];

回答by software evolved

I also saw an oddity where adding the standard singleton code espoused by apple prevented the class from being loaded. The code was working as expected, then I added the singleton, and suddenly the NSClassFromString started returning nil. Commenting out the singleton code resulted in the NSClassFromString resolving the class correctly. I don't understand the interaction, but I think the singleton static var was somehow getting mangled to hide the class name...?

我还看到了一个奇怪的地方,即添加 Apple 支持的标准单例代码会阻止加载该类。代码按预期工作,然后我添加了单例,突然 NSClassFromString 开始返回 nil。注释掉单例代码会导致 NSClassFromString 正确解析类。我不理解交互,但我认为单例静态变量不知何故被破坏以隐藏类名......?