objective-c 按名称创建objective-c 类实例?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1174093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:57:55  来源:igfitidea点击:

Create objective-c class instance by name?

objective-creflection

提问by Mark

Is it possible to create an instance of a class by name? Something like:

是否可以按名称创建类的实例?就像是:

NSString* className = @"Car";
id* p = [Magic createClassByName:className];
[p turnOnEngine];

I don't know if this is possible in objective-c but seems like it would be,

我不知道这在 Objective-c 中是否可行,但似乎是这样,

回答by Chris McCall

id object = [[NSClassFromString(@"NameofClass") alloc] init];

回答by Simon Woodside

NSClassFromString()runs the risk of mistyping the class name or otherwise using a class that doesn't exist. You won't find out until runtime if you make that error. Instead, if you use the built-in objective-c type of Classto create a variable, then the compiler will verify that the class exists.

NSClassFromString()冒着错误输入类名或以其他方式使用不存在的类的风险。如果你犯了那个错误,你直到运行时才会发现。相反,如果您使用内置的objective-c 类型Class来创建变量,那么编译器将验证该类是否存在。

For example, in your .h:

例如,在您的.h

@property Class NameOfClass;

and then in your .m:

然后在你的.m

id object = [[NameOfClass alloc] init];

If you mistyped the class name or if it doesn't exist, you'll get an error at compile time. Also I think this is cleaner code.

如果你输入错误的类名或者它不存在,你会在编译时得到一个错误。另外我认为这是更干净的代码。

回答by Mark

If you are working with Objective-C without the NeXTstep(OS X, iOS, GNUstepetc) system or you just think this method is cleaner, then you could utilize the Objective-C language runtime library's API. Under Objective-C 2.0:

如果您正在使用Objective-C的工作没有NeXTstepOS XiOSGNUstep等)的系统,或者你只是觉得这个方法更清洁,那么你可以利用Objective-C语言运行时库的API。下Objective-C 2.0

#import <objc/runtime.h>
//Declaration in the above named file
id objc_getClass(const char* name);
//Usage
id c = objc_getClass("Object");
[ [ c alloc ] free ];

Under the Objective-C (1.0 or unnamed version) you would utilize the following:

在 Objective-C(1.0 或未命名版本)下,您将使用以下内容:

#import <objc/objc-api.h>
//Declaration within the above named file
Class objc_get_class( const char* name);
//Usage
Class cls = objc_get_class( "Test" );
id obj = class_create_instance( cls );
[ obj free ];

I haven't tested the 1.0version, however I have used the 2.0function in code that is now in production. I personally believe utilizing the 2.0function is cleaner if available than the NS function as it consumes less space: the length of the name in bytes + 1 ( null terminator )for the 2.0 API versus the sum of two pointers (isa, cstring), a size_t length (cstring_length), and the length of the string in bytes + 1for the NeXTSTEPAPI.

我还没有测试过这个1.0版本,但是我已经2.0在现在生产的代码中使用了这个功能。我个人认为利用该2.0功能是清洁如果有比NS功能,因为它消耗更少的空间:the length of the name in bytes + 1 ( null terminator )对于2.0 API相比the sum of two pointers (isa, cstring),一size_t length (cstring_length),和length of the string in bytes + 1NeXTSTEPAPI。

回答by Skela

@interface Magic : NSObject
+ (id)createInstanceOfClass:(Class)classe;
@end

@implementation Magic

+ (id)createInstanceOfClass:(Class)classe
{
    return [[classe alloc] init];
}

@end

Then to use it:

然后使用它:

Car *car = [Magic createInstanceOfClass:[Car class]];
[car engineTurnOn];