美文网首页
iOS-runtime使用总结

iOS-runtime使用总结

作者: LJMagicCoder | 来源:发表于2017-09-11 14:30 被阅读0次

关联

static const char LJKey = '\0';
objc_setAssociatedObject(self, &LJKey, header, OBJC_ASSOCIATION_ASSIGN); 
objc_getAssociatedObject(self, &LJKey);

获取类名

+ (NSString *)xx_getClassName:(Class)class {
const char *className = class_getName(class);
return [NSString stringWithUTF8String:className];
}

获取属性列表(公有和私有)

+ (NSArray *)xx_getPropertyList:(Class)class {
unsigned int count = 0;
objc_property_t *propertyList = class_copyPropertyList(class, &count);

NSMutableArray *list = [NSMutableArray arrayWithCapacity:count];
for (unsigned int i = 0; i < count; i++ ) {
    const char *propertyName = property_getName(propertyList[i]);
    [list addObject:[NSString stringWithUTF8String: propertyName]];
}
free(propertyList);
return [NSArray arrayWithArray:list];
}

获取成员变量

+ (NSArray *)xx_getIvarList:(Class)class {
unsigned int count = 0;
Ivar *ivarList = class_copyIvarList(class, &count);
NSMutableArray *list = [NSMutableArray arrayWithCapacity:count];
for (unsigned int i = 0; i < count; i++ ) {
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:2];
    const char *ivarName = ivar_getName(ivarList[i]);
    const char *ivarType = ivar_getTypeEncoding(ivarList[i]);
    dic[@"ivarType"] = [NSString stringWithUTF8String:ivarType];
    dic[@"ivarName"] = [NSString stringWithUTF8String:ivarName];
    [list addObject:dic];
}
free(ivarList);
return [NSArray arrayWithArray:list];
}

修改对象指针

object_setClass(object, [modifyClass class]);

方法交换(Method Swizzling)

Method firstMethod = class_getInstanceMethod(class, firstSEL);
Method secondMethod = class_getInstanceMethod(class, secondSEL);
method_exchangeImplementations(firstMethod, secondMethod);

发送消息

objc_msgSend(testObject,@selector(methodWithString:),@"objc_msgSend");
//根据sel_registerName返回一个方法 == SEL
objc_msgSend(testObject,sel_registerName("methodWithString:"),@"sel_registerName");

获取协议列表

+ (NSArray *)xx_getProtocolList:(Class)class {
unsigned int count = 0;
__unsafe_unretained Protocol **protocolList = class_copyProtocolList(class, &count);
NSMutableArray *list = [NSMutableArray arrayWithCapacity:count];
for (unsigned int i = 0; i < count; i++ ) {
    const char *protocolName = protocol_getName(protocolList[i]);
    [list addObject:[NSString stringWithUTF8String: protocolName]];
}
return [NSArray arrayWithArray:list];
}

相关文章

  • iOS-runtime使用总结

    关联 获取类名 获取属性列表(公有和私有) 获取成员变量 修改对象指针 方法交换(Method Swizzling...

  • 壹、面试复习OC篇之runtime

    暂时copy过来,过后添加自己理解 原文地址:iOS-runtime通篇详解-上 iOS-runtime通篇详解-...

  • iOS开发 runtime理解

    http://yimouleng.com/2015/05/28/ios-runtime/

  • iOS-RunTime介绍及使用

    一、RunTime概念 RunTime简称运行时,我们总是听说OC是动态语言运行时机制,也就是系统在运行时候的一些...

  • iOS-Runtime原理及使用

    Runtime原理 1.Runtime简称运行时.OC就是运行时机制,(就是系统在运行的时候的一些机制)其中最主要...

  • Runtime学习日程

    1、Runtime全方位装逼指南 2、Runtime窥探 (一)| 基本介绍 3、iOS-runtime通篇详解-...

  • iOS-runtime通篇详解-下

    上接上篇iOS-runtime通篇详解-上 原创内容,转载请注明出处: http://www.jianshu.co...

  • iOS-RunTime

    https://www.jianshu.com/p/19f280afcb24 <简书 — 刘小壮> https:/...

  • iOS-Runtime

    一.什么是runtime? 话说每次面试都问runtime,你不会runtime能拿到像我66k的工资吗?所以关于...

  • iOS-runtime

    一、基本概念 Runtime基本是用C和汇编写的,可见苹果为了动态系统的高效而作出的努力。你可以在这里下到苹果维护...

网友评论

      本文标题:iOS-runtime使用总结

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