在Objective-C中动态加载类? [英] Dynamically Load Classes in Objective-C?

查看:152
本文介绍了在Objective-C中动态加载类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在运行时从Objective-C中的字符串中导入一个类?任何产生类似结果的方法也将受到欢迎。

Is there a way to #import a class from a string in Objective-C at run time? Any methods that would produce a similar result would also be welcome.

编辑:

我想要访问在运行时确定其名称的类。这样的东西:

I want access to a class whose name I determine at runtime. So something like this:

NSString *className = getClassName();
Class myClass = loadClass(className);
myClass *myVar = [[myClass alloc] init];

有没有办法做到这一点没有静态的#import指令myClass在文件?

Is there any way to do this without putting a static #import directive for myClass at the top of the file?

推荐答案

#import 指令不会导入它将指定文件中的文本插入当前文件。这显然在源代码被编译后在运行时是没有用的。

The #import directive does not "import" a class — it inserts the text from the named file into the current file. This obviously isn't useful at runtime after the source has been compiled.

你想要的是创建一个包和动态加载群组。为了能够与核心程序中的类进行通信,你可能需要一些捆绑类实现的通用协议。

What you want is to create a bundle with the classes and dynamically load the bundle. In order to be able to talk to the classes from the core program, you'll probably want to have some common protocol that bundled classes implement.

这篇关于在Objective-C中动态加载类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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