Class type Objective C

后端 未结 2 1711
自闭症患者
自闭症患者 2021-02-09 03:03

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

-(Class) class

What type of object is the Class object? Or

2条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-09 03:47

    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.

提交回复
热议问题