api
const char *name OBJC2_UNAVAILABLE;
long version OBJC2_UNAVAILABLE;
OBJC_EXPORT const char *class_getName(Class cls)
__OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
OBJC_EXPORT int class_getVersion(Class cls)
__OSX_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
OBJC_EXPORT void class_setVersion(Class cls, int version)
__OSX_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
应用
@interface FBAnimal : NSObject
@end
@implementation FBAnimal
@end
- (void)class_info
{
NSObject *object = [[NSObject alloc] init];
FBAnimal *animal = [[FBAnimal alloc] init];
Class objectClsObj = object_getClass(object);
Class objectClsObj_isa = object_getClass(objectClsObj);
Class animalClsObj = object_getClass(animal);
Class animalClsObj_isa = object_getClass(animalClsObj);
NSLog(@"objectClsObj name: %s, objectMetaClsObj name: %s", class_getName(objectClsObj), class_getName(objectClsObj_isa));
NSLog(@"animalClsObj name: %s, animalMetaClsObj name: %s", class_getName(animalClsObj), class_getName(animalClsObj_isa));
NSLog(@"objectClsObj ver: %d, objectMetaClsObj ver: %d", class_getVersion(objectClsObj), class_getVersion(objectClsObj_isa));
NSLog(@"animalClsObj ver: %d, animalMetaClsObj ver: %d", class_getVersion(animalClsObj), class_getVersion(animalClsObj_isa));
class_setVersion(objectClsObj, 5);
class_setVersion(objectClsObj_isa, 8);
class_setVersion(animalClsObj, 5);
class_setVersion(animalClsObj_isa, 8);
NSLog(@"objectClsObj ver: %d, objectMetaClsObj ver: %d", class_getVersion(objectClsObj), class_getVersion(objectClsObj_isa));
NSLog(@"animalClsObj ver: %d, animalMetaClsObj ver: %d", class_getVersion(animalClsObj), class_getVersion(animalClsObj_isa));
}
output:
objectClsObj name: NSObject, objectMetaClsObj name: NSObject
animalClsObj name: FBAnimal, animalMetaClsObj name: FBAnimal
objectClsObj ver: 0, objectMetaClsObj ver: 7
animalClsObj ver: 0, animalMetaClsObj ver: 7
objectClsObj ver: 5, objectMetaClsObj ver: 8
animalClsObj ver: 5, animalMetaClsObj ver: 8
总结
- class object和meta-class object的name一致,为类名
- class object默认version为0,meta-class object默认version为7
- class object和meta-class object的version改变是运行时,非持久性