Objective-C class - > string like: [NSArray className] - > @"NSArray"
Estoy tratando de obtener un nombre de cadena de una clase del objeto de clase en sí.
// For instance
[NSArray className]; // @"NSArray"
He encontrado object_getClassName(id obj)
pero eso requiere que se le pase una instancia, y en mi caso eso es trabajo innecesario.
Entonces, ¿cómo puedo obtener una cadena de un objeto de clase, y no una instancia?
123
Author: Jonathan Sterling, 2010-02-25
3 answers
NSString *name = NSStringFromClass ([NSArray class]);
Incluso puedes volver hacia el otro lado:
Class arrayClass = NSClassFromString (name);
id anInstance = [[arrayClass alloc] init];
296
Author: dreamlax,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-02-25 06:04:04
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-02-25 06:04:04
Aquí hay una manera diferente de hacerlo con un poco menos de escribir:
NSString *name = [NSArray description];
1
Author: Sherwin Zadeh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-12-28 22:56:47
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-12-28 22:56:47
Considere esta alternativa:
const char *name = class_getName(cls);
Es mucho más rápido, ya que no tiene que asignar el objeto NSString y convertir ASCII a cualquier representación NSString
. Así es como se implementa NSStringFromClass()
.
0
Author: wonder.mice,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-09-26 18:11:59
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-09-26 18:11:59