1 Star 0 Fork 0

宝东/iOS_RunTime

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
NSArray+Safe.m 1.47 KB
一键复制 编辑 原始数据 按行查看 历史
//
// NSArray+Safe.m
// KVOTest
//
// Created by StriEver on 16/7/29.
// Copyright © 2016年 StriEver. All rights reserved.
//
#import "NSArray+Safe.h"
#import <objc/runtime.h>
#import "NSObject+ImpChangeTool.h"
@implementation NSArray (Safe)
+ (void)load{
static dispatch_once_t onceDispatch;
dispatch_once(&onceDispatch, ^{
[self SwizzlingMethod:@"objectAtIndex:" systemClassString:@"__NSArrayI" toSafeMethodString:@"st_objectAtIndex:" targetClassString:@"NSArray"];
[self SwizzlingMethod:@"initWithObjects:count:" systemClassString:@"__NSPlaceholderArray" toSafeMethodString:@"initWithObjects_st:count:" targetClassString:@"NSArray"];
//
[self SwizzlingMethod:@"arrayByAddingObject:" systemClassString:@"__NSArrayI" toSafeMethodString:@"arrayByAddingObject_st:" targetClassString:@"NSArray"];
});
}
- (id)st_objectAtIndex:(NSUInteger)index{
//判断数组是否越界
if (index >= [self count]) {
return nil;
}
return [self st_objectAtIndex:index];
}
- (NSArray *)arrayByAddingObject_st:(id)anObject {
if (!anObject) {
return self;
}
return [self arrayByAddingObject_st:anObject];
}
- (instancetype)initWithObjects_st:(id *)objects count:(NSUInteger)count {
NSUInteger newCount = 0;
for (NSUInteger i = 0; i < count; i++) {
if (!objects[i]) {
break;
}
newCount++;
}
self = [self initWithObjects_st:objects count:newCount];
return self;
}
@end
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/donggehaoya/iOS_RunTime.git
git@gitee.com:donggehaoya/iOS_RunTime.git
donggehaoya
iOS_RunTime
iOS_RunTime
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385