在Objective-C类名的NSString创建对象 [英] Create object from NSString of class name in Objective-C

查看:118
本文介绍了在Objective-C类名的NSString创建对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在想,如果我能创造出一些类的一个对象,如果我有类的名称
在的NSString。
我知道这是可能的动作一样,C#和PHP等语言...

事情是这样的:

 的NSString *的className = @AwesomeViewController
*的UIViewController对象= [[类名的alloc] initWithNibName:捆绑的className:无];


解决方案

类是在对象 - 第一类对象了。您可以使用<一个一个NSString获取类对象href=\"http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Functions/Reference/reference.html#//apple_ref/doc/uid/20000055-BCIJDAJA\">NSClassFromString功能。

  [NSClassFromString(的className)ALLOC] INIT ...]

I was wondering if i could create a object of some class if i have the name of the class in a NSString. I know this is possible in other languages like ActionScript, C# and PHP...

Something like this:

NSString *className = @"AwesomeViewController";
UIViewController *object = [[className alloc] initWithNibName:className bundle:nil];

解决方案

Classes are first-class objects in Obj-C too. You can get the class object from an NSString with the NSClassFromString function.

[[NSClassFromString(className) alloc] init...]

这篇关于在Objective-C类名的NSString创建对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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