简体   繁体   English

我如何从NSString获得课程?

[英]How do I get class from NSString?

for example, I have 例如,我有

NSString *myClass = @"RootViewController"

How do I get class from NSString? 我如何从NSString获得课程?

试试这个

 id obj= [[NSClassFromString(@"RootViewController") alloc] init];

You can use 您可以使用

Class class = NSClassFromString(@"RootViewController");

NSClassFromString NSClassFromString

Obtains a class by name. 按名称获得一个班级。

Class NSClassFromString ( NSString *aClassName ); NSClassFromString类(NSString * aClassName);

Parameters 参数

aClassName aClassName

 The name of a class. 

Return Value 回报价值

The class object named by aClassName, or nil if no class by that name is currently loaded. 由aClassName命名的类对象,如果当前没有加载该名称的类,则为nil。 If aClassName is nil, returns nil. 如果aClassName为nil,则返回nil。 Availability 可用性

 Available in iOS 2.0 and later. 

There is a lot of methods like this in foundation framework 在基础框架中有很多这样的方法

NSGetSizeAndAlignment
NSClassFromString
NSStringFromClass
NSSelectorFromString
NSStringFromSelector
NSStringFromProtocol
NSProtocolFromString

Refer Foundation Functions Reference 参考基础功能参考

Class class = NSClassFromString(@"RootViewController");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM