創(chuàng)建一個NSArray的類別添加到項目中,類別中使用 runtime 將系統(tǒng)的方法替換為你自己實現(xiàn)的方法
//.h
#import <Foundation/Foundation.h>
@interface NSArray (Beyond)
@end
//.m
#import "NSArray+Beyond.h"
#import <objc/runtime.h>
@implementation NSArray (Beyond)
+ (void)load
{
[super load];
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
//替換空數(shù)組方法
[self class:objc_getClass("__NSArray0") exchangeImplementation:@selector(objectAtIndex:) withMethod:@selector(emptyObjectIndex:)];
[self class:objc_getClass("__NSSingleObjectArrayI") exchangeImplementation:@selector(objectAtIndex:) withMethod:@selector(singleObjectIndex:)];
//替換不可變數(shù)組方法
[self class:objc_getClass("__NSArrayI") exchangeImplementation:@selector(objectAtIndex:) withMethod:@selector(objectAtSafeIndex:)];
[self class:objc_getClass("__NSArrayI") exchangeImplementation:@selector(objectAtIndexedSubscript:) withMethod:@selector(objectAtSafeIndexSubscript:)];
//替換可變數(shù)組方法
[self class:objc_getClass("__NSArrayM") exchangeImplementation:@selector(objectAtIndex:) withMethod:@selector(mutableObjectAtSafeIndex:)];
[self class:objc_getClass("__NSArrayM") exchangeImplementation:@selector(objectAtIndexedSubscript:) withMethod:@selector(mutableObjectAtSafeIndexSubscript:)];
});
}
+ (void)class:(Class)class exchangeImplementation:(SEL)oldSEL withMethod:(SEL)newSEL
{
Method oldMethod = class_getInstanceMethod(class, oldSEL);
Method newMethod = class_getInstanceMethod(class, newSEL);
method_exchangeImplementations(oldMethod, newMethod);
}
- (id)emptyObjectIndex:(NSUInteger)index
{
NSLog(@"__NSArray0 取一個空數(shù)組 objectAtIndex , 崩潰") ;
return nil;
}
- (id)singleObjectIndex:(NSInteger)index {
if (index >= self.count || !self.count) {
NSLog(@"__NSSingleObjectArrayI 取一個不可變單元素數(shù)組時越界 objectAtIndex , 崩潰") ;
return nil;
}
return [self singleObjectIndex:index];
}
- (id)objectAtSafeIndex:(NSUInteger)index
{
if (index > self.count - 1 || !self.count) {
@try {
return [self objectAtSafeIndex:index];
}
@catch (NSException *exception) {
NSLog(@"exception: %@", exception.reason);
return nil;
}
}else {
return [self objectAtSafeIndex:index];
}
}
- (id)objectAtSafeIndexSubscript:(NSUInteger)index
{
if (index > self.count - 1 || !self.count) {
@try {
return [self objectAtSafeIndexSubscript:index];
}
@catch (NSException *exception) {
NSLog(@"exception: %@", exception.reason);
return nil;
}
}else {
return [self objectAtSafeIndexSubscript:index];
}
}
- (id)mutableObjectAtSafeIndex:(NSUInteger)index
{
if (index > self.count - 1 || !self.count) {
@try {
return [self mutableObjectAtSafeIndex:index];
}
@catch (NSException *exception) {
NSLog(@"exception: %@", exception.reason);
return nil;
}
}else {
return [self mutableObjectAtSafeIndex:index];
}
}
- (id)mutableObjectAtSafeIndexSubscript:(NSUInteger)index
{
if (index > self.count - 1 || !self.count) {
@try {
return [self mutableObjectAtSafeIndexSubscript:index];
}
@catch (NSException *exception) {
NSLog(@"exception: %@", exception.reason);
return nil;
}
}else {
return [self mutableObjectAtSafeIndexSubscript:index];
}
}
@end
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者