Objective-C class -> string like: [NSArray className] -> @“NSArray”

后端 未结 3 1106
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-07 08:15

I am trying to get a string name of a class from the class object itself.

// For instance
[NSArray className]; // @\"NSArray\"

I have found

相关标签:
3条回答
  • 2020-12-07 08:36

    Here's a different way to do it with slightly less typing:

    NSString *name = [NSArray description];
    
    0 讨论(0)
  • 2020-12-07 08:43
    NSString *name = NSStringFromClass ([NSArray class]);
    

    You can even go back the other way:

    Class arrayClass = NSClassFromString (name);
    id anInstance = [[arrayClass alloc] init];
    
    0 讨论(0)
  • 2020-12-07 08:55

    Consider this alternative:

    const char *name = class_getName(cls);
    

    It's much faster, since it doesn't have to alloc NSString object and convert ASCII to whatever NSString representation is. That's how NSStringFromClass() is implemented.

    0 讨论(0)
提交回复
热议问题