Runtime

2016-11-22  本文已影响10人  AlexDing
用法
//动态创建属性
objc_setAssociatedObject(self, (__bridge const void *)(KpropertyKey), temp.copy, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
//获取动态创建的属性值
objc_getAssociatedObject(self, (__bridge const void *)(KpropertyKey));
unsigned int count = 0;
objc_property_t *properties = class_copyPropertyList(self, &count);
NSMutableArray *temp = [NSMutableArray array];
for (unsigned int i = 0; i<count; i++) {
    objc_property_t property = properties[i];
    const char *cStr = property_getName(property);
    NSString *ocStr = [NSString stringWithCString:cStr encoding:NSUTF8StringEncoding];
    [temp addObject:ocStr];
}
free(properties);//注意释放内存
//原有方法
Method old = class_getInstanceMethod(self, @selector(setImage:));
//新方法
    Method new = class_getInstanceMethod(self, @selector(DD_setImage:));
//交叉方法
    method_exchangeImplementations(old, new);
上一篇 下一篇

猜你喜欢

热点阅读