Runtime第三篇-对象操作

1,动态创建对象

1.1  id class_createInstance ( Class cls, size_t  extraBytes );

1.2  id objc_constructInstance ( Class cls, void *bytes );

1.3 void * objc_destructInstance ( id obj ); //销毁一个实例

2,把父类对象转换为子类对象

NSObject *a = [[NSObject alloc] init];

id newB = object_copy(a, class_getInstanceSize(MyClass.class));

object_setClass(newB, MyClass.class);

object_dispose(a);

//MyClass继承自NSObject。

3,读写对象的实例变量

3.1 修改实例变量的值

Ivar object_setInstanceVariable ( id obj, const char *name, void *value );

void object_setIvar ( id obj, Ivar ivar, id value );

3.2 获取实例变量的值

Ivar object_getInstanceVariable ( id obj, const char *name, void **outValue );

id object_getIvar ( id obj, Ivar ivar );

3.3 获取对象的类

Class object_getClass ( id obj );

3.4 设置对象的类

Class object_setClass ( id obj, Class cls );

3.5 返回对象的类名

const char * object_getClassName ( id obj );

你可能感兴趣的:(Runtime第三篇-对象操作)