Swizzle运行时
2016-05-21 15:26
267 查看
#import <Foundation/Foundation.h>
//获取一个类的所有属性名字:类型的名字,具有@property的,
父类的获取不了!
NSDictionary *GetPropertyListOfObject(NSObject *object);
NSDictionary *GetPropertyListOfClass(Class cls);
void Swizzle(Class c, SEL origSEL,
SEL newSEL)
//
// ObjcRuntime.m
// EMeal_Cook
//
// Created by Wujg on 15/10/15.
// Copyright © 2015年 Wujg. All rights reserved.
//
#import "ObjcRuntime.h"
#import <objc/runtime.h>
NSDictionary *GetPropertyListOfObject(NSObject *object) {
return
GetPropertyListOfClass([object
class]);
}
NSDictionary *GetPropertyListOfClass(Class cls) {
NSMutableDictionary *dict = [NSMutableDictionary
dictionary];
unsigned
int outCount, i;
objc_property_t *properties =
class_copyPropertyList(cls, &outCount);
for(i =
0; i < outCount; i++) {
objc_property_t property = properties[i];
const
char *propName =
property_getName(property);
const
char *propType =
property_getAttributes(property);
if(propType&&propName) {
NSArray *anAttribute = [[NSString
stringWithUTF8String:propType]componentsSeparatedByString:@","];
NSString *aType = anAttribute[0];
[dict setObject:aType
forKey:[NSString
stringWithUTF8String:propName]];
}
}
free(properties);
return dict;
}
//静态就交换静态,实例方法就交换实例方法
void Swizzle(Class c, SEL origSEL,
SEL newSEL) {
Method origMethod =
class_getInstanceMethod(c, origSEL);
Method newMethod =
nil;
if (!origMethod) {
origMethod = class_getClassMethod(c, origSEL);
if (!origMethod) {
return;
}
newMethod = class_getClassMethod(c, newSEL);
if (!newMethod) {
return;
}
} else {
newMethod = class_getInstanceMethod(c, newSEL);
if (!newMethod) {
return;
}
}
//自身已经有了就添加不成功,直接交换即可
if(class_addMethod(c, origSEL,
method_getImplementation(newMethod),
method_getTypeEncoding(newMethod))) {
class_replaceMethod(c, newSEL,
method_getImplementation(origMethod),
method_getTypeEncoding(origMethod));
} else {
method_exchangeImplementations(origMethod, newMethod);
}
}
//获取一个类的所有属性名字:类型的名字,具有@property的,
父类的获取不了!
NSDictionary *GetPropertyListOfObject(NSObject *object);
NSDictionary *GetPropertyListOfClass(Class cls);
void Swizzle(Class c, SEL origSEL,
SEL newSEL)
//
// ObjcRuntime.m
// EMeal_Cook
//
// Created by Wujg on 15/10/15.
// Copyright © 2015年 Wujg. All rights reserved.
//
#import "ObjcRuntime.h"
#import <objc/runtime.h>
NSDictionary *GetPropertyListOfObject(NSObject *object) {
return
GetPropertyListOfClass([object
class]);
}
NSDictionary *GetPropertyListOfClass(Class cls) {
NSMutableDictionary *dict = [NSMutableDictionary
dictionary];
unsigned
int outCount, i;
objc_property_t *properties =
class_copyPropertyList(cls, &outCount);
for(i =
0; i < outCount; i++) {
objc_property_t property = properties[i];
const
char *propName =
property_getName(property);
const
char *propType =
property_getAttributes(property);
if(propType&&propName) {
NSArray *anAttribute = [[NSString
stringWithUTF8String:propType]componentsSeparatedByString:@","];
NSString *aType = anAttribute[0];
[dict setObject:aType
forKey:[NSString
stringWithUTF8String:propName]];
}
}
free(properties);
return dict;
}
//静态就交换静态,实例方法就交换实例方法
void Swizzle(Class c, SEL origSEL,
SEL newSEL) {
Method origMethod =
class_getInstanceMethod(c, origSEL);
Method newMethod =
nil;
if (!origMethod) {
origMethod = class_getClassMethod(c, origSEL);
if (!origMethod) {
return;
}
newMethod = class_getClassMethod(c, newSEL);
if (!newMethod) {
return;
}
} else {
newMethod = class_getInstanceMethod(c, newSEL);
if (!newMethod) {
return;
}
}
//自身已经有了就添加不成功,直接交换即可
if(class_addMethod(c, origSEL,
method_getImplementation(newMethod),
method_getTypeEncoding(newMethod))) {
class_replaceMethod(c, newSEL,
method_getImplementation(origMethod),
method_getTypeEncoding(origMethod));
} else {
method_exchangeImplementations(origMethod, newMethod);
}
}
相关文章推荐
- Android开发实践 数据存储 学习、应用、总结
- 在代码中改变GridLayout的子控件的位置
- java线程池
- 输入子系统
- ScrollView嵌套ListView冲突问题的最优解决方案
- elasticsearch es分布式集群初探
- JAVA并发编程学习笔记之AQS源码分析(共享与互斥)
- 排序算法个人总结(4)
- 在使用cppunit,搭建测试环境时遇到的问题以及解决办法(仅供参考)
- iOS开发笔记--Core Bluetooth开发
- elasticsearch java客户端 - 原生esClient
- Geekban极客班 第二周
- office2016 下载和激活
- elasticsearch java客户端 - Jest
- mysqldump常用命令总结
- lein auto reload 异常
- Spring(三)——核心容器Bean
- 求斐波那契数列的前10项,并逆序输出,用数组实现
- JAVA并发编程学习笔记之AQS源码分析(超时、中断与其他)
- 第十三周阅读程序 虚析构函数