iOS runtime之?dāng)?shù)據(jù)、字典越界及button重復(fù)點擊處理

一、runtime之?dāng)?shù)據(jù)、字典越界

方法交換

Runtime解決數(shù)據(jù)越界及字典key或value為nil的情況,主要通過Runtime的方法交換實現(xiàn),可以擴(kuò)展一下NSObject分類:

@implementation NSObject (FlyElephant)

- (void)swizzleMethod:(SEL)originalSelector swizzledSelector:(SEL)swizzledSelector{

Class class = [self class];

Method originalMethod = class_getInstanceMethod(class, originalSelector);

Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);


//可能方法不在這個類中,可能在父類中,因此嘗試添加方法實現(xiàn)

BOOL didAddMethod = class_addMethod(class,

originalSelector,

method_getImplementation(swizzledMethod),

method_getTypeEncoding(swizzledMethod));


//嘗試添加方法實現(xiàn) 成功

if (didAddMethod) {

class_replaceMethod(class,

swizzledSelector,

method_getImplementation(originalMethod),

method_getTypeEncoding(originalMethod));

} else {//嘗試添加方法實現(xiàn) 失敗,說明已經(jīng)存在這個方法,則可以直接交換方法的實現(xiàn)

method_exchangeImplementations(originalMethod, swizzledMethod);

}

}

@end


擴(kuò)展NSArray和NSDictionary分類,實現(xiàn)方法交換:

NSArray0表示一般空數(shù)組,NSArrayI表示一般數(shù)組,__NSArrayM可變數(shù)組,NSArray和NSMutableArray相當(dāng)于工廠,最終實現(xiàn)通過上面三個類進(jìn)行實現(xiàn)的,有興趣的可以自行了解一下類簇

@implementation NSArray (FlyElephant)

+ (void)load{

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{

@autoreleasepool {

[objc_getClass("__NSArray0") swizzleMethod:@selector(objectAtIndex:) swizzledSelector:@selector(emptyObjectIndex:)];

[objc_getClass("__NSArrayI") swizzleMethod:@selector(objectAtIndex:) swizzledSelector:@selector(arrObjectIndex:)];

[objc_getClass("__NSArrayM") swizzleMethod:@selector(objectAtIndex:) swizzledSelector:@selector(mutableObjectIndex:)];

[objc_getClass("__NSArrayM") swizzleMethod:@selector(insertObject:atIndex:) swizzledSelector:@selector(mutableInsertObject:atIndex:)];

}

});

}

- (id)emptyObjectIndex:(NSInteger)index{

return nil;

}

- (id)arrObjectIndex:(NSInteger)index{

if (index >= self.count || index < 0) {

return nil;

}

return [self arrObjectIndex:index];

}

- (id)mutableObjectIndex:(NSInteger)index{

if (index >= self.count || index < 0) {

return nil;

}

return [self mutableObjectIndex:index];

}

- (void)mutableInsertObject:(id)object atIndex:(NSUInteger)index{

if (object) {

[self mutableInsertObject:object atIndex:index];

}

}

@end

@implementation NSDictionary (FlyElephant)

+ (void)load{

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{

@autoreleasepool {

[objc_getClass("__NSDictionaryM") swizzleMethod:@selector(setObject:forKey:) swizzledSelector:@selector(mutableSetObject:forKey:)];

}

});

}

- (void)mutableSetObject:(id)obj forKey:(NSString *)key{

if (obj && key) {

[self mutableSetObject:obj forKey:key];

}

}

@end

通過Runtime進(jìn)行對數(shù)組字典進(jìn)行方法交換之后,之前對鍵盤輸入的場景沒有完全弄清楚,完整過程應(yīng)該是,文本框輸入文字→Home鍵進(jìn)入后臺→點擊App圖標(biāo)重新進(jìn)入→崩潰,有的時候前兩步就直接崩潰了

**[UIKeyboardLayoutStar release]: message sent to deallocated instance 0x1459e0600**

因此Runtime的這個文件需要通過mrc管理:

一般項目都是 ARC 模式,需要單獨問Runtime的這個問題添加MRC支持,

Build Phases -> Compile Sources找到文件設(shè)置 -fno-objc-arc 標(biāo)簽。

如果項目是MRC 模式,則為 ARC 模式的代碼文件加入 -fobjc-arc 標(biāo)簽.

同時可以為相應(yīng)的代碼塊添加autoreleasepool:

@autoreleasepool{if(index >=self.count|| index <0) {returnnil;? ? ? ? }return[selfarrObjectIndex:index];? ? }


引用鏈接1:http://m.itdecent.cn/p/5492d2d3342b?

引用鏈接2:http://www.cnblogs.com/chushenruhua/p/5667580.html



二、避免重復(fù)惡意點擊

#import#define defaultInterval 0.5? //默認(rèn)時間間隔@interface UIButton (Swizzling)@property (nonatomic, assign) NSTimeInterval timeInterval;@end#import "UIButton+Swizzling.h"#import#import "NSObject+Swizzling.h"

@interface UIButton()

/**bool 類型 YES 不允許點擊? NO 允許點擊? 設(shè)置是否執(zhí)行點UI方法*/

@property (nonatomic, assign) BOOL isIgnoreEvent;

@end

@implementation UIButton (Swizzling)

+(void)load

{

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{

[objc_getClass("UIButton") swizzleSelector:@selector(sendAction:to:forEvent:) withSwizzledSelector:@selector(customSendAction:to:forEvent:)];

});

}

- (void)customSendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event

{

if ([NSStringFromClass(self.class) isEqualToString:@"UIButton"]) {

self.timeInterval =self.timeInterval ==0 ?defaultInterval:self.timeInterval;

if (self.isIgnoreEvent){

return;

}else if (self.timeInterval > 0){

[self performSelector:@selector(resetState) withObject:nil afterDelay:self.timeInterval];

}

}

//此處 methodA和methodB方法IMP互換了,實際上執(zhí)行 sendAction;所以不會死循環(huán)

self.isIgnoreEvent = YES;

[self customSendAction:action to:target forEvent:event];

}

- (NSTimeInterval)timeInterval

{

return [objc_getAssociatedObject(self, _cmd) doubleValue];

}

- (void)setTimeInterval:(NSTimeInterval)timeInterval

{

objc_setAssociatedObject(self, @selector(timeInterval), @(timeInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);

}

//runtime 動態(tài)綁定 屬性

- (void)setIsIgnoreEvent:(BOOL)isIgnoreEvent{

// 注意BOOL類型 需要用OBJC_ASSOCIATION_RETAIN_NONATOMIC 不要用錯,否則set方法會賦值出錯

objc_setAssociatedObject(self, @selector(isIgnoreEvent), @(isIgnoreEvent), OBJC_ASSOCIATION_RETAIN_NONATOMIC);

}

- (BOOL)isIgnoreEvent{

//_cmd == @select(isIgnore); 和set方法里一致

return [objc_getAssociatedObject(self, _cmd) boolValue];

}

- (void)resetState{

[self setIsIgnoreEvent:NO];

}

@end

著作權(quán)歸作者所有,轉(zhuǎn)載請聯(lián)系作者獲得授權(quán),并標(biāo)注“簡書作者”。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 轉(zhuǎn)至元數(shù)據(jù)結(jié)尾創(chuàng)建: 董瀟偉,最新修改于: 十二月 23, 2016 轉(zhuǎn)至元數(shù)據(jù)起始第一章:isa和Class一....
    40c0490e5268閱讀 2,106評論 0 9
  • 我們常常會聽說 Objective-C 是一門動態(tài)語言,那么這個「動態(tài)」表現(xiàn)在哪呢?我想最主要的表現(xiàn)就是 Obje...
    Ethan_Struggle閱讀 2,351評論 0 7
  • 本文轉(zhuǎn)載自:http://yulingtianxia.com/blog/2014/11/05/objective-...
    ant_flex閱讀 893評論 0 1
  • 本文詳細(xì)整理了 Cocoa 的 Runtime 系統(tǒng)的知識,它使得 Objective-C 如虎添翼,具備了靈活的...
    lylaut閱讀 870評論 0 4
  • 轉(zhuǎn)載:http://yulingtianxia.com/blog/2014/11/05/objective-c-r...
    F麥子閱讀 843評論 0 2

友情鏈接更多精彩內(nèi)容