項目中針對使用NSArray的objectAtIndex:函數(shù)時第步,數(shù)組越界問題尾菇,可以用category配合runtime的黑魔法 Method Swizzing解決。
#import "NSArray+Checking.h"
#import <objc/runtime.h>
@implementation NSArray (Checking)
+ (void)load{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [self class];
Method objAtIndex = class_getInstanceMethod(class, @selector(objectAtIndex:));
Method objAtIndexCheck = class_getInstanceMethod(class, @selector(objAtIndexCheck:));
BOOL didAddMethod = class_addMethod([self class], @selector(objAtIndexCheck:), method_getImplementation(objAtIndexCheck), method_getTypeEncoding(objAtIndexCheck));
if (didAddMethod) {
class_replaceMethod([self class], @selector(objAtIndexCheck:), method_getImplementation(objAtIndex), method_getTypeEncoding(objAtIndex));
} else {
method_exchangeImplementations(objAtIndex, objAtIndexCheck);
}
});
}
- (id)objAtIndexCheck:(NSInteger)index{
if (self.count <= index) {
return nil;
}
return [self objAtIndexCheck:index];
}
@end
這樣當(dāng)調(diào)用【NSArray objectAtIndex:】時橄浓,就會先先調(diào)用objAtIndexCheck:份氧,然后做一些自己的判斷,再去調(diào)用系統(tǒng)的函數(shù)诗茎。
但是問題來了
上面的代碼函數(shù)objAtIndexCheck:并沒有被成功調(diào)用工坊,檢查了一下午,無數(shù)次的驗證敢订,仍然沒有結(jié)果王污。
我懷疑寫法問題?還是這種被廣大程序猿吹噓的方法根本就是錯的楚午?
然后我進行了嘗試
//在一個類里面這樣寫玉掸。
Method objAtIndex = class_getInstanceMethod(self.class, @selector(aa));
Method objAtIndexCheck = class_getInstanceMethod(self.class, @selector(bb));
method_exchangeImplementations(objAtIndex, objAtIndexCheck);
[self aa];
[self bb];
完全沒有問題。
我問了朋友醒叁,朋友給了我一篇文章
作者遇到了和我一樣的問題司浪,他進行了如下嘗試,也沒有問題把沼。(我沒測試)
+(void)load{
static dispatch_once_t onceToken ;
dispatch_once(&onceToken, ^{
Method m1 = class_getInstanceMethod([self class], @selector(lastObject));
Method m2 = class_getInstanceMethod([self class], @selector(My_lastObject));
BOOL isAdd = class_addMethod([self class], @selector(lastObject), method_getImplementation(m2), method_getTypeEncoding(m2));
if (isAdd == YES) {
class_replaceMethod([self class], @selector(My_lastObject), method_getImplementation(m1), method_getTypeEncoding(m1));
}else{
method_exchangeImplementations(m1, m2);
}
Method m3 = class_getInstanceMethod([self class], @selector(firstObject));
Method m4 = class_getInstanceMethod([self class], @selector(My_fistObjcet));
method_exchangeImplementations(m3, m4);
});
}
-(id)My_fistObjcet{
NSLog(@"My_fistObjcet : exchangeSuccess");
return [self My_fistObjcet];
}
-(id)My_lastObject{
NSLog(@"My_lastObject : exchangeSuccess");
return [self My_lastObject];
}
-------結(jié)論------
并不是不支持啊易,而是使用錯了類名,對于class_getInstanceMethod函數(shù)第一個參數(shù)的class饮睬,使用的不是[self class]租谈,而應(yīng)該是需要的類alloc init之后的真實類,NSArray對應(yīng)__NSArrayI捆愁,NSMutableArray對應(yīng)__NSArrayM割去,而后面的函數(shù)class_addMethod、class_replaceMethod使用的則是[self class]昼丑。進行如下修改就沒問題了
Class class = NSClassFromString(@"__NSArrayI");
Method objAtIndex = class_getInstanceMethod(class, @selector(objectAtIndex:));
Method objAtIndexCheck = class_getInstanceMethod(class, @selector(objAtIndexCheck:));
BOOL didAddMethod = class_addMethod([self class], @selector(objAtIndexCheck:), method_getImplementation(objAtIndexCheck), method_getTypeEncoding(objAtIndexCheck));
if (didAddMethod) {
class_replaceMethod([self class], @selector(objAtIndexCheck:), method_getImplementation(objAtIndex), method_getTypeEncoding(objAtIndex));
} else {
method_exchangeImplementations(objAtIndex, objAtIndexCheck);
}
NSArray Method Swizzing.png
對于OC數(shù)組來說呻逆,NSArray確實是基類,[NSArray class]是NSArray菩帝,但是NSArray *array = [[NSArray alloc] init];之后咖城,這時的array的類就不是NSArray了而是
__NSArrayI
茬腿。具體為什么不是很清楚,應(yīng)該是OC的類簇的問題宜雀。
所以先找到實例的具體類切平,然后再確定參數(shù)的class
NSString類.png
/**
* Returns a specified instance method for a given class.
*
* @param cls The class you want to inspect.
* @param name The selector of the method you want to retrieve.
*
* @return The method that corresponds to the implementation of the selector specified by
* \e name for the class specified by \e cls, or \c NULL if the specified class or its
* superclasses do not contain an instance method with the specified selector.
*
* @note This function searches superclasses for implementations, whereas \c class_copyMethodList does not.
*/
OBJC_EXPORT Method class_getInstanceMethod(Class cls, SEL name)
__OSX_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
/**
* Adds a new method to a class with a given name and implementation.
*
* @param cls The class to which to add a method.
* @param name A selector that specifies the name of the method being added.
* @param imp A function which is the implementation of the new method. The function must take at least two arguments—self and _cmd.
* @param types An array of characters that describe the types of the arguments to the method.
*
* @return YES if the method was added successfully, otherwise NO
* (for example, the class already contains a method implementation with that name).
*
* @note class_addMethod will add an override of a superclass's implementation,
* but will not replace an existing implementation in this class.
* To change an existing implementation, use method_setImplementation.
*/
OBJC_EXPORT BOOL class_addMethod(Class cls, SEL name, IMP imp,
const char *types)
__OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
最后同樣測試了一下NSString,同樣的情況辐董。
#import "NSString+Check.h"
#import <objc/runtime.h>
@implementation NSString (Check)
+ (void)load{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = NSClassFromString(@"__NSCFConstantString");
Method objAtIndex = class_getInstanceMethod(class, @selector(isEqualToString:));
Method objAtIndexCheck = class_getInstanceMethod(class, @selector(isEqualToStringCheck:));
BOOL didAddMethod = class_addMethod([self class], @selector(isEqualToStringCheck:), method_getImplementation(objAtIndexCheck), method_getTypeEncoding(objAtIndexCheck));
if (didAddMethod) {
class_replaceMethod([self class], @selector(isEqualToStringCheck:), method_getImplementation(objAtIndex), method_getTypeEncoding(objAtIndex));
} else {
method_exchangeImplementations(objAtIndex, objAtIndexCheck);
}
});
}
- (BOOL)isEqualToStringCheck:(NSString *)aString{
NSLog(@"aaa = %@",aString);
return [self isEqualToStringCheck:aString];
}
@end