在运行时创建一个类 - 为什么 NSClassFromString AND objc_getclass 返回 nil?
我尝试使用两者:
NSClassFromString and objc_getclass
返回一个类,因此我可以在运行时创建它,但是对于某些类,这两个函数都返回 nil,例如“TestClass”。请注意,NSClassFromString 对我来说适用于 99% 的类。
如果我
[TestClass class];
在调用 NSStringFromClass 或 objc_getclass 之前添加它,它就会起作用。如果我尝试使用类引用创建类,即:
[TestClass alloc];
它也有效。那么如何强制该类在运行时加载,以便 NSClassFromString 或 objc_getclass 不会返回 nil?
I've tried using both:
NSClassFromString and objc_getclass
to return a class, so I can create it at runtime, but both functions return nil for some classes, for example "TestClass". Note that NSClassFromString works for me for 99% of classes.
If I add
[TestClass class];
before I call NSStringFromClass or objc_getclass it, it works. and if I try to just create the class using a class reference, i.e.:
[TestClass alloc];
it works too. So how can I force the class to load at runtime so NSClassFromString or objc_getclass will not return nil?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我遇到了同样的问题。 使用 Which 来查找类
就我而言,我有代码在 Foo 类与我的 AppDelegate 位于同一项目包中时
。作为重构代码的一部分,我将 Foo 类和关联的模型类从 AppDelegate 项目移出到一个公共静态 lib 项目中,这样更容易测试和重用。
当我将 Foo 类移至 libFooBar 时,objc_getClass("Foo") 现在返回 nil 而不是 Foo 类。
为了解决这个问题,我调用了我感兴趣的类,以便 libFooBar 中的类注册到 Objective-C 运行时。我这样做了:
现在 objc_getClass("Foo") 再次返回 Foo 类而不是 nil。
我只能假设静态库类在调用静态库中的类之一之前不会注册。
I ran into this same problem. In my case I had code which was looking up the class using
Which worked when the Foo class was in the same project bundle as my AppDelegate.
As part of refactoring the code, I moved the Foo class and associated model classes out of the AppDelegate project into a common static lib project that would be easier to test and reuse.
When I moved the Foo class into libFooBar the objc_getClass("Foo") now returned nil instead of the Foo class.
To solve this, I put in call to the class I am interested in so that the classes from libFooBar are registered with the Objective-C runtime. I did this like so:
Now the objc_getClass("Foo") returns the Foo class again instead of nil.
I can only assume that the static library classes are not registered until a call to one of the classes in the static library is made.
Objective-C 运行时参考可以在这方面为您提供帮助。例如,objc_getClass 的文档说:“指定类的 Class 对象,如果该类未在 Objective-C 运行时注册,则为 nil”。环顾四周关于注册的讨论,你会在 objc_getClassList 中找到这个花絮:
(当然,文档已经过时了,因为 objc_addClass 已被弃用,而 objc_allocateClassPair 和 objc_registerClassPair 已被弃用。)
如果您不让自己变得轻松,这看起来会是一个充满伤害的世界。当应用程序启动时,您有机会在代码中引用动态类吗?
The Objective-C Runtime Reference can help you here. For example, the documentation for objc_getClass says, "The Class object for the named class, or nil if the class is not registered with the Objective-C runtime." Looking around for discussion of registration you find this tidbit in objc_getClassList:
(And of course the docs are out of date because objc_addClass is deprecated, objc_allocateClassPair and objc_registerClassPair in its place.)
This looks like a world of hurt if you don't make this easy on yourself. Any chance you can just reference your dynamic classes in code when the app starts?
我必须手动将类(例如 TestClass.m)添加到构建设置中。构建阶段>编译源> +>测试类.m
I had to manually add the class (e.g. TestClass.m) to the build settings. Build Phases > Compile Sources > + > TestClass.m