id object_copy(id obj, size_t size) // 对象释放
id object_dispose(id obj)
TsetClass *obj = [TsetClassnew];
id objTest =object_copy(obj,sizeof(obj));
(lldb)po &obj
0x00007fff54a7cb08
(lldb)po &objTest
0x00007fff54a7cb00
无疑问打印的内存地址不是一样的,此方法类似指针的深拷贝
list:TestRuntime(1034,0x111b66300) malloc: * error for object 0x7ffe1a75b940: pointer being freed was not allocated
TsetClass *obj1 = [TsetClass new];
Class aClass =object_setClass(obj1, [CustomClassOther class]);
(lldb)po obj1
<CustomClassOther: 0x7f8e4bd14480>
(lldb) po aClass
TsetClass
有没有出乎你的想象0.0.
TsetClass *obj2 = [TsetClass new];
Class aLogClass =object_getClass(obj2);
(lldb) po aLogClass
TsetClass
NSString *className = [NSStringstringWithCString:object_getClassName(obj2)encoding:NSUTF8StringEncoding];
NSLog(@"className:%@", className);
(lldb) po className
TsetClass
BOOL class_addMethod(Class cls,SEL name,IMP imp, const char *types)
TsetClass *instance = [[TsetClassalloc]init];
方法添加
class_addMethod([TsetClass class],@selector(ocMethod:), (IMP)cfunction,"i@:@");
if ([instance respondsToSelector:@selector(ocMethod:)]) {
NSLog(@"Yes, instance respondsToSelector:@selector(ocMethod:)");
} else
{
NSLog(@"Sorry");
}
int a = (int)[instance ocMethod:@"我是一个OC的method,C函数实现"];
NSLog(@"a:%d", a);
int cfunction(id self, SEL _cmd, NSString *str) {
NSLog(@"%@", str);
return 520;//随便返回个值
}
获取一个类的所有方法/所有属性
获取一个类的所有方法
u_int count;
Method* methods= class_copyMethodList([UIViewController class], &count);
for (int i = 0; i < count ; i++) {
SEL name = method_getName(methods[i]);
NSString *strName = [NSString stringWithCString:sel_getName(name)encoding:NSUTF8StringEncoding];
// NSLog(@"%@",strName);
}
获取一个类的所有属性
u_int count;
objc_property_t *properties=class_copyPropertyList([TsetClass class], &count);
for (int i = 0; i < count ; i++) {
const char* propertyName =property_getName(properties[i]);
NSString *strName = [NSString stringWithCString:propertyName encoding:NSUTF8StringEncoding];
NSLog(@"%@",strName);
}
float myFloatValue;
object_getInstanceVariable(self,"myFloat"(void*)&myFloatValue);
NSLog(@"%f", myFloatValue);
float newValue = 10.00f;
unsigned int addr = (unsigned int)&newValue;
object_setInstanceVariable(self,"myFloat", *(float**)addr);
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/qq_26544491/article/details/48064487