xcode NSClassFromString() 总是返回 nil
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24570345/
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
NSClassFromString() always returns nil
提问by tesla
The following code prints nil
, despite ListCell
is a valid class.
nil
尽管ListCell
是一个有效的类,但下面的代码会打印出 。
var lCellClass : AnyClass! = NSClassFromString("ListCell");
println(lCellClass);
The docs are saying that method returns
文档说该方法返回
The class object named by aClassName, or
nil
if no class by that name is currently loaded. If aClassNameisnil
, returnsnil
.
由aClassName命名的类对象,或者
nil
如果当前没有加载该名称的类。如果aClassName是nil
,则返回nil
。
I also tried to get NSClassFromString()
of current viewcontroller which is LOADED but still get nil
我还尝试获取NSClassFromString()
当前已加载但仍获取的视图控制器nil
What can be the problem ?
可能是什么问题?
Update:Even trying to do this NSClassFromString("Array")
I still get nil
更新:即使尝试这样做NSClassFromString("Array")
我仍然得到nil
回答by Hyman Lawrence
The NSClassFromString
function doeswork for (pure and Objective-C-derived) swift classes, but only if you use the fully qualified name.
该NSClassFromString
函数确实适用于(纯和 Objective-C 派生的)swift 类,但前提是您使用完全限定名称。
For example, in a module called MyApp
with a pure swift class called Person
:
例如,在一个使用名为MyApp
的纯 swift 类调用的模块中Person
:
let personClass: AnyClass? = NSClassFromString("MyApp.Person")
The module name is defined by the "Product Module Name" (PRODUCT_MODULE_NAME
) build setting, typically the same name as your target (with some normalization applied to e.g. remove spaces).
模块名称由“产品模块名称”( PRODUCT_MODULE_NAME
) 构建设置定义,通常与您的目标名称相同(应用了一些规范化以例如删除空格)。
This is atypical, but if the class you're loading is in the current module and you don't know the module name at compile-time e.g. because the code is compiled as part of multiple targets, you can look up the bundle name dynamically, which usuallycorresponds to the module name:
这是不典型的,但是如果您加载的类在当前模块中并且您在编译时不知道模块名称,例如因为代码被编译为多个目标的一部分,您可以动态查找包名称,通常对应于模块名称:
let moduleName = Bundle.main.infoDictionary!["CFBundleName"] as! String
let personClass: AnyClass? = NSClassFromString(moduleName + "." + "Person")
I don't recommend this however unless you really don't know at compile-time.
但是我不推荐这样做,除非你在编译时真的不知道。
See Using Swift Class Names with Objective-C APIsin Using Swift With Cocoa and Objective-Cfor more information.
见与Objective-C的API的使用斯威夫特类名称在使用雨燕与可可和Objective-C的更多信息。
回答by Osmund
It's also possible to specify a name for the symbol in Objective-C which omits the namespace in objective C by using the @objc annotation. If you prefer to use the name without the module, just use the same class name:
也可以在 Objective-C 中为符号指定一个名称,通过使用 @objc 注释省略目标 C 中的命名空间。如果您更喜欢使用没有模块的名称,只需使用相同的类名:
@objc(Foobar)
class Foobar{
}
NSClassFromString("Foobar") will return the class Foobar.
NSClassFromString("Foobar") 将返回类 Foobar。
回答by Rinku
This may be problem that your class have not extend from NSObject. As by default swift does not have superclass.
这可能是您的类没有从 NSObject 扩展的问题。默认情况下,swift 没有超类。