IOS 使用runtime swizzling(黑魔法的那些事)
来源:互联网 发布:网络歌手 金达莱花 编辑:程序博客网 时间:2024/05/17 23:38
1.为了防止数组越界崩溃和字典添加空对象崩溃,扩展了以下两个类导致的问题。
突然发现如果我在键盘出现的状态下,按下Home键使得App从Foreground切换到Background的时候,App就会触发一个exception导致crash,这个crash的信息在我看来很奇怪。
[UIKeyboardLayoutStar release]: message sent to deallocated instance
最开始,我想,干脆去掉这个库,不用了。去掉后,发现运行App就出错,出错的原因是某处代码会把nil给设置到NSMutableDictionary中,而NSMutableDictionary不能储存nil。结果,我还不能直接把该库给从工程中移除。
- 初始化所有的NSError变量声明,原代码都是NSError *err;的形式,在ARC下没问题的,compiler都给自动初始化为nil了,但是MRC下不行
- 把NSError从函数返回的时候,加上autorelease
按照上面两点改好后,再到build phase里给那几个m文件加上-fno-objc-arc flag
接下来又测试了几次,UIKeyboardLayoutStar release这个exception没有再出现,问题解决。
顺便再多说几句,DurexKit的作者也提到了某几个文件不要用ARC。
CUSLayout2014-04-09 17:48:58@habib狂鳄– : 这两个发现一个缺陷,已经修正了,请试一下最新版,最新版如果用源码需要把NSObject+SafeKit标记为-fno-objc-arc,或者直接用静态库
#import "NSArray+Swizzling.h"
#import <objc/runtime.h>
@implementation NSArray (Swizzling)
+ (void)load
{
[superload];
//1.目标方法
Method fromMethod =class_getInstanceMethod(objc_getClass("__NSArrayI"),@selector(objectAtIndex:));
//2.修改方法
Method toMethod =class_getInstanceMethod(objc_getClass("__NSArrayI"),@selector(lzx_objectAtIndex:));
//3.交换方法
method_exchangeImplementations(fromMethod, toMethod);
Method fromMMethod =class_getInstanceMethod(objc_getClass("__NSArrayM"),@selector(objectAtIndex:));
//2.修改方法
Method toMMethod =class_getInstanceMethod(objc_getClass("__NSArrayM"),@selector(lzxm_objectAtIndex:));
//3.交换方法
method_exchangeImplementations(fromMMethod, toMMethod);
}
/**
用来输出数组越界崩溃信息
@param index 索引
@return 返回数据
*/
- (id)lzx_objectAtIndex:(NSInteger)index
{
if (index >=self.count)
{
//异常处理
NSLog(@"-----------------------crash -----------------------\n");
NSLog(@"reason : [%@ %@] index {%lu} beyond bounds [0...%lu]",
NSStringFromClass([self class]),
NSStringFromSelector(_cmd),
(unsignedlong)index,MAX((unsignedlong)self.count -1, 0));
returnnil;
}
else
{
return [selflzx_objectAtIndex:index];
}
}
- (id)lzxm_objectAtIndex:(NSInteger)index
{
if (index >=self.count)
{
//异常处理
NSLog(@"[%@ %@] index {%lu} beyond bounds [0...%lu]",
NSStringFromClass([self class]),
NSStringFromSelector(_cmd),
(unsignedlong)index,MAX((unsignedlong)self.count -1, 0));
returnnil;
}
else
{
return [selflzxm_objectAtIndex:index];
}
}
@end
#import "NSMutableDictionary+Swizzling.h"
#import <objc/runtime.h>
@implementation NSMutableDictionary (Swizzling)
+ (void)load
{
[superload];
//1.目标方法
Method fromMethod =class_getInstanceMethod(objc_getClass("__NSDictionaryM"),@selector(setObject:forKey:));
//2.修改方法
Method toMethod =class_getInstanceMethod(objc_getClass("__NSDictionaryM"),@selector(swl_setObject:forKey:));
//3.交换方法
method_exchangeImplementations(fromMethod, toMethod);
}
/**
用来输出字典设置空对象错误信息
@param swlObject 目标对象
@param key 关键字
*/
- (void)swl_setObject:(id)swlObject forKey:(NSString *)key
{
if (swlObject ==nil)
{
@try {
[selfswl_setObject:swlObject forKey:key];
} @catch (NSException *exception) {
//错误处理
NSLog(@"----------------------crash----------------------\n");
NSLog(@"reson : --- %s crash Method %s---------\n", class_getName(self.class),__func__);
NSLog(@"%@",[exception callStackSymbols]);
swlObject = [NSStringstringWithFormat:@""];
[selfswl_setObject:swlObject forKey:key];
} @finally {
}
}
else
{
[selfswl_setObject:swlObject forKey:key];
}
}
@end
- IOS 使用runtime swizzling(黑魔法的那些事)
- iOS黑魔法--Method Swizzling
- 123.Runtime之黑魔法-Method Swizzling
- Swizzling:偷梁换柱的黑魔法!
- Method Swizzling(黑魔法)
- iOS黑魔法-Method Swizzling
- iOS黑魔法-Method Swizzling
- iOS黑魔法-Method Swizzling
- iOS黑魔法-Method Swizzling
- iOS黑魔法之Method Swizzling
- iOS黑魔法-Method Swizzling
- iOS黑魔法-Method Swizzling
- iOS黑魔法-Method Swizzling
- iOS黑魔法-Method Swizzling
- iOS黑魔法-Method Swizzling
- runtime学习之 - 黑魔法 Swizzling,改变系统方法!
- 苹果的“黑魔法”Method Swizzling
- iOS开发之原来Runtime的黑魔法这么厉害
- 记录自己的支付集成(微信&支付宝)
- scan方法
- keepalive自启动服务报错,问题的解决
- 顺序结构小结——顺序表(柔性数组版 上)
- ES6学习笔记1:变量提升以及函数提升
- IOS 使用runtime swizzling(黑魔法的那些事)
- Ubuntu16.04+cuda8.0+cudnnV5.1 caffe SSD配置、训练及测试过程
- react路由配置
- 23switch
- 再讲*.hbm.xml中的类型映射
- C语言之简单计算器
- 对kafka的初步了解
- Linux学习笔记:grep及正则表达式
- Android学习笔记——蓝牙入门