數組越界問題,解決方法也簡單,就是使用runtime進行方法交換,在自定義方法中進行規避越界的情況。
+ (void)load{
[super load];
Method fromMethod = class_getInstanceMethod(objc_getClass("__NSArrayI"), @selector(objectAtIndex:));
Method toMethod = class_getInstanceMethod(objc_getClass("__NSArrayI"), @selector(xz_objectAtIndex:));
method_exchangeImplementations(fromMethod, toMethod);
}
- (id)xz_objectAtIndex:(NSUInteger)index{
// 數組為空或索引超出范圍時,進行規避越界行為
if (!self.count || index >= self.count) {
@try{
return [self xz_objectAtIndex:index];
}
@catch(NSException *exception){
NSLog(@"method:%@,exception:%@",NSStringFromSelector(_cmd),exception);
return nil;
}
}
return [self xz_objectAtIndex:index];
}
但可能也沒那么簡單,這里當數組為空的時候,程序依然會崩潰,提示
index 1 beyond bounds for empty NSArray
這里怎么就不好用了呢,原來當數組為空的時候,類型是__NSSingleObjectArrayI,而不是__NSArrayI。
這里就牽扯到一個知識點:類簇;在Objc中有許多類簇,比如NSNumber、NSString、NSArray。
什么是類簇?將一些相似的類組合在一個公共的、抽象的超類下面,使用的時候,判斷使用的條件返回具體的類的實例,是一種抽象的工廠模式。例如NSNumber,是類int,char,Bool的超類工廠類,在內部NSNumber會轉化成NSInt類的具體工廠 。
圖片.png
數組的類簇包括哪些呢?
- 1.數組為空的時候__NSArray0
- 2.數組只有一個元素的時候__NSSingleObjectArrayI
- 3.數組有多個元素的時候__NSArrayI
- 4.可變數組__NSArrayM
NSArray *array1 = [[NSArray alloc]init];
NSArray *array2 = [NSArray array];
NSArray *array3 = @[@1,@2];
NSArray *array4 = [NSArray arrayWithObject:@"1"];
// po 之后
(lldb) po array1
<__NSArray0 0x7fff80617ad0>(
)
(lldb) po array2
<__NSArray0 0x7fff80617ad0>(
)
(lldb) po array3
<__NSArrayI 0x6000015dcf00>(
1,
2
)
(lldb) po array4
<__NSSingleObjectArrayI 0x6000017d41d0>(
1
)
NSMutableArray *mutaArray = [NSMutableArray array];
NSMutableArray *mutaArray1 = [NSMutableArray arrayWithObject:@"1"];
NSMutableArray *mutaArray2 = [NSMutableArray arrayWithArray:@[@1,@2,@3,@4]];
(lldb) po mutaArray
<__NSArrayM 0x600001bd56b0>(
)
(lldb) po mutaArray1
<__NSArrayM 0x600001bd5560>(
1
)
(lldb) po mutaArray2
<__NSArrayM 0x600001bd5770>(
1,
2,
3,
4
)
所以對數據的越界行為,進行runtime方法交換的時候,需要考慮__NSArray0、__NSSingleObjectArrayI、__NSArrayI、__NSArrayM,這幾種類型。
代碼修改為
+ (void)load{
[super load];
// 因為數組是類簇類型的類,集合了幾種相似的類,所以要考慮多種情況
// 1,當數組為空的時候,類為__NSArray0
Method fromEmptyArrayMethod = class_getInstanceMethod(objc_getClass("__NSArray0"), @selector(objectAtIndex:));
Method toEmptyArrayMethod = class_getInstanceMethod(objc_getClass("__NSArray0"), @selector(xz_empty_objectAtIndex:));
method_exchangeImplementations(fromEmptyArrayMethod, toEmptyArrayMethod);
// 2,當數組只有一個元素的時候,類為__NSSingleObjectArrayI
Method fromSingleArrayMethod = class_getInstanceMethod(objc_getClass("__NSSingleObjectArrayI"), @selector(objectAtIndex:));
Method toSingleArrayMethod = class_getInstanceMethod(objc_getClass("__NSSingleObjectArrayI"), @selector(xz_signle_objectAtIndex:));
method_exchangeImplementations(fromSingleArrayMethod, toSingleArrayMethod);
// 3,當不可變數組有多個元素的時候,類為__NSArrayI
Method fromArrayMehod = class_getInstanceMethod(objc_getClass("__NSArrayI"), @selector(objectAtIndex:));
Method toArrayMethod = class_getInstanceMethod(objc_getClass("__NSArrayI"), @selector(xz_objectAtIndex:));
method_exchangeImplementations(fromArrayMehod, toArrayMethod);
// 4,當可變數組有多個元素的時候,類為__NSArrayM
Method fromMutableArrayMethod = class_getInstanceMethod(objc_getClass("__NSArrayM"), @selector(objectAtIndex:));
Method toMutableArrayMethod = class_getInstanceMethod(objc_getClass("__NSArrayM"), @selector(xz_mutable_objectAtIndex:));
method_exchangeImplementations(fromMutableArrayMethod, toMutableArrayMethod);
}
#pragma mark - 可變數組:類為__NSArrayM
- (id)xz_mutable_objectAtIndex:(NSUInteger)index{
// 數組為空或索引超出范圍時,進行規避越界行為
if (!self.count || index >= self.count) {
@try{
return [self xz_mutable_objectAtIndex:index];
}
@catch(NSException *exception){
NSLog(@"method:%s\n,exception:%@",__func__,exception);
return nil;
}
}
return [self xz_mutable_objectAtIndex:index];
}
#pragma mark - 不可變數組:類為__NSArrayI
- (id)xz_objectAtIndex:(NSUInteger)index{
// 數組為空或索引超出范圍時,進行規避越界行為
if (!self.count || index >= self.count) {
@try{
return [self xz_objectAtIndex:index];
}
@catch(NSException *exception){
NSLog(@"method:%s\n,exception:%@",__func__,exception);
return nil;
}
}
return [self xz_objectAtIndex:index];
}
#pragma mark - 單個元素數組:類為__NSSingleObjectArrayI
- (id)xz_signle_objectAtIndex:(NSUInteger)index{
// 數組為空或索引超出范圍時,進行規避越界行為
if (!self.count || index >= self.count) {
@try{
return [self xz_signle_objectAtIndex:index];
}
@catch(NSException *exception){
NSLog(@"method:%s\n,exception:%@",__func__,exception);
return nil;
}
}
return [self xz_signle_objectAtIndex:index];
}
#pragma mark - 空數組:類為__NSArray0
- (id)xz_empty_objectAtIndex:(NSUInteger)index{
// 數組為空或索引超出范圍時,進行規避越界行為
if (!self.count || index >= self.count) {
@try{
return [self xz_empty_objectAtIndex:index];
}
@catch(NSException *exception){
NSLog(@"method:%s\n,exception:%@",__func__,exception);
return nil;
}
}
return [self xz_empty_objectAtIndex:index];
}
- 知識點:類簇、抽象工廠模式
- 參考文章:iOS 類簇(Class Cluster)使用心得