NSClassFromString()始终返回nil [英] NSClassFromString() always returns nil

查看:430
本文介绍了NSClassFromString()始终返回nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码打印 nil ,尽管 ListCell 是有效的类。

The following code prints nil, despite ListCell is a valid class.

var lCellClass : AnyClass! = NSClassFromString("ListCell");

println(lCellClass);

文档说方法返回


aClassName 命名的类对象,如果当前没有加载该名称的类,则为 nil 。如果 aClassName nil ,则返回 nil

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

我还试图得到当前viewcontroller的 NSClassFromString(),它是LOADED但仍然得到没有

I also tried to get NSClassFromString() of current viewcontroller which is LOADED but still get nil

可能是什么问题?

更新:
即使尝试这样做 NSClassFromString(Array)我仍然得到 nil

推荐答案

NSClassFromString 函数 适用于(纯和Objective-C派生的)swift类,但仅限于使用完全限定名称。

The NSClassFromString function does work for (pure and Objective-C-derived) swift classes, but only if you use the fully qualified name.

例如,在名为 MyApp的项目中使用名为的纯swift类

For example, in a project called MyApp with a pure swift class called Person:

let personClass: AnyClass? = NSClassFromString("MyApp.Person")

请参阅使用带有Objective-C的Swift类名API 使用Swift与Cocoa和Objective-C 获取更多信息。

See Using Swift Class Names with Objective-C APIs in Using Swift With Cocoa and Objective-C for more information.

这篇关于NSClassFromString()始终返回nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆