Class type Objective C

感情迁移 提交于 2020-01-01 03:02:17

问题


In the NSObject protocol, it defines a method that is similar to this:

-(Class) class

What type of object is the Class object? Or is it even an object? What can I do with the object? Can I get the base class or adopted protocols?


回答1:


Class is itself a class defined by the Objective-C runtime, akin to the Class class in Java. For example, you can use the function class_getClassName() to get the name of a class:

NSObject *o = [[[NSObject alloc] init] autorelease];
NSLog(@"%s\n", class_getClassName([o class]));  // prints "NSObject"

You can do all kinds of introspection/reflection with Class objects; see the Objective-C runtime reference for details.




回答2:


It is now

NSObject *o = [[NSObject alloc]init];
NSLog(@"%s\n", object_getClassName([o class]));

object_getClassName instead of class_getClassName



来源:https://stackoverflow.com/questions/3539014/class-type-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!