Runtime

作者: AlexDing | 来源:发表于2016-11-22 15:58 被阅读10次
用法
  • 关联对象
//动态创建属性
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);

相关文章

网友评论

      本文标题:Runtime

      本文链接:https://www.haomeiwen.com/subject/fxappttx.html