iOS runtime运行时特性解决数组越界问题。
2017-08-29 22:06
246 查看
@interface NSObject (Exchange)
-(void)swizzleMethod:(SEL)OldSel withNewMethod:(SEL)NewSel;
@end#import
"NSObject+Exchange.h"
@implementation NSObject (Exchange)
-(void)swizzleMethod:(SEL)OldSel withNewMethod:(SEL)NewSel
{
Class selfclass=[self class];
Method oldMethod=class_getInstanceMethod(selfclass, OldSel);
Method newMethod=class_getInstanceMethod(selfclass, NewSel);
BOOL addselector=class_addMethod(selfclass, OldSel, class_getMethodImplementation(selfclass, NewSel), method_getTypeEncoding(newMethod));
if (addselector) {
NSLog(@"加方法成功");
class_replaceMethod(selfclass, NewSel, class_getMethodImplementation(selfclass, OldSel), method_getTypeEncoding(oldMethod));
}else {
method_exchangeImplementations(oldMethod, newMethod);
} }
#import "NSObject+Exchange.h"
@interface NSArray (CheckIndex)
@end
#import "NSArray+CheckIndex.h"
@implementation NSArray (CheckIndex)
+(void)load{
SEL oldSel=@selector(objectAtIndex:);
SEL newSel=@selector(emptycheckObjectAtIndex:);
SEL newSel1=@selector(arrayIcheckObjectAtIndex:);
SEL newSel2=@selector(arrayMcheckObjectAtIndex:);
[objc_getClass("__NSArray0") swizzleMethod:oldSel withNewMethod:newSel];
[objc_getClass("__NSArrayI") swizzleMethod:oldSel withNewMethod:newSel1];
[objc_getClass("__NSArrayM") swizzleMethod:oldSel withNewMethod:newSel2];
[objc_getClass("__NSSingleObjectArrayI")
swizzleMethod:@selector(objectAtIndex:)
swizzledSelector:@selector(singleObjectIndex:)];
}
-(id)emptycheckObjectAtIndex:(NSInteger)index{
if (self.count>index) {
return [self emptycheckObjectAtIndex:index]; }else{ return nil;
}
}
-(id)arrayIcheckObjectAtIndex:(NSUInteger)index{
NSLog(@"检查");
if (self.count>index) { return [self arrayIcheckObjectAtIndex:index];
}else{
return nil;
}}
-(id)arrayMcheckObjectAtIndex:(NSUInteger)index{
NSLog(@"检查"); if (self.count>index) {
return [self arrayMcheckObjectAtIndex:index];
}else{
return nil;
}
}
-(id)singleObjectIndex:(NSInteger )index
{
if (index >=
self.count || index <
0) {
return
nil;
}
return [self
singleObjectIndex:index];
}
-(void)swizzleMethod:(SEL)OldSel withNewMethod:(SEL)NewSel;
@end#import
"NSObject+Exchange.h"
@implementation NSObject (Exchange)
-(void)swizzleMethod:(SEL)OldSel withNewMethod:(SEL)NewSel
{
Class selfclass=[self class];
Method oldMethod=class_getInstanceMethod(selfclass, OldSel);
Method newMethod=class_getInstanceMethod(selfclass, NewSel);
BOOL addselector=class_addMethod(selfclass, OldSel, class_getMethodImplementation(selfclass, NewSel), method_getTypeEncoding(newMethod));
if (addselector) {
NSLog(@"加方法成功");
class_replaceMethod(selfclass, NewSel, class_getMethodImplementation(selfclass, OldSel), method_getTypeEncoding(oldMethod));
}else {
method_exchangeImplementations(oldMethod, newMethod);
} }
#import "NSObject+Exchange.h"
@interface NSArray (CheckIndex)
@end
#import "NSArray+CheckIndex.h"
@implementation NSArray (CheckIndex)
+(void)load{
SEL oldSel=@selector(objectAtIndex:);
SEL newSel=@selector(emptycheckObjectAtIndex:);
SEL newSel1=@selector(arrayIcheckObjectAtIndex:);
SEL newSel2=@selector(arrayMcheckObjectAtIndex:);
[objc_getClass("__NSArray0") swizzleMethod:oldSel withNewMethod:newSel];
[objc_getClass("__NSArrayI") swizzleMethod:oldSel withNewMethod:newSel1];
[objc_getClass("__NSArrayM") swizzleMethod:oldSel withNewMethod:newSel2];
[objc_getClass("__NSSingleObjectArrayI")
swizzleMethod:@selector(objectAtIndex:)
swizzledSelector:@selector(singleObjectIndex:)];
}
-(id)emptycheckObjectAtIndex:(NSInteger)index{
if (self.count>index) {
return [self emptycheckObjectAtIndex:index]; }else{ return nil;
}
}
-(id)arrayIcheckObjectAtIndex:(NSUInteger)index{
NSLog(@"检查");
if (self.count>index) { return [self arrayIcheckObjectAtIndex:index];
}else{
return nil;
}}
-(id)arrayMcheckObjectAtIndex:(NSUInteger)index{
NSLog(@"检查"); if (self.count>index) {
return [self arrayMcheckObjectAtIndex:index];
}else{
return nil;
}
}
-(id)singleObjectIndex:(NSInteger )index
{
if (index >=
self.count || index <
0) {
return
nil;
}
return [self
singleObjectIndex:index];
}
相关文章推荐
- iOS_解决demo真机运行不了问题
- 解决Unity 3D 在运行iOS程序时出现mscorlib.dll找不到的问题
- java-基础入门-泛型数组列表-解决运行时动态更改数组的问题
- iOS瀑布流(UIScrollView或UITableView)和解决运行不流畅的问题
- java-基础入门-泛型数组列表-解决运行时动态更改数组的问题
- 关于iOS UITableView 数据源数组加载完成之后,滑动时出现cell为空的问题解决笔记
- IOS开发问题解决之Xcode运行程序时attaching to挂起不动
- iOS 关于数组越界的解决方法
- iOS小技巧:用runtime 解决UIButton 重复点击问题
- [解决问题集合]数组越界
- iOS小技巧–用runtime 解决UIButton 重复点击问题
- 用数组的方法解决大数、巨数的阶乘结果越界的问题
- iOS小技巧:用runtime 解决UIButton 重复点击问题
- iOS小技巧--用runtime 解决UIButton 重复点击问题
- 今日遇到的javascript问题解决-数组越界和jquery取iframe的问题。
- iOS小技巧--用runtime 解决UIButton 重复点击问题
- DELPHI XE5 UP2 运行IOS 遇到 Wrapper init failed: (null)问题的解决办法
- iOS 用Runtime解决服务器返回NSNull问题
- 解决了一个ios真机上的内存越界问题
- IOS上解决内存越界访问问题