有没有一种方法可以按名称获取课程? [英] Is there a way to get a class by name?

查看:93
本文介绍了有没有一种方法可以按名称获取课程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Objective-C中,当您将类的名称作为字符串时,是否可以获取类并向其发送消息?例如,是否有函数func其中func(@"NSString") == [NSString class]?

In Objective-C, is there a way to get a class and send messages to it when you have the name of the class as a string? For example, is there a function func where func(@"NSString") == [NSString class]?

我想知道的原因是我正在为我正在使用的语言构建动态链接程序库,并且希望它具有与Objective-C库的接口.

The reason that I want to know this is I am building a dynamic linker library for a language I am working on, and I want it to have an interface to Objective-C libraries.

推荐答案

是的-实际上是两个.如果您有基金会类型的框架(例如来自Cocoa,Cocoa Touch或GNUstep的框架),则可以使用

Yes — two, in fact. If you have a Foundation-type framework (e.g. from Cocoa, Cocoa Touch or GNUstep), you can use the NSClassFromString() function, which is precisely the same as your func. If you do not want to depend on a framework, there's a similar runtime function, objc_getClass(), that takes a const char* and returns the named class (or nil if none is found).

这篇关于有没有一种方法可以按名称获取课程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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