From e3af99b9c4409d52a0600cbafce5279bd773c178 Mon Sep 17 00:00:00 2001 From: yulingtianxia Date: Thu, 22 Mar 2018 21:00:44 +0800 Subject: [PATCH] 1.1.2 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复一处继承链关系判断问题 --- MTDemo/MTDemo/ViewController.m | 2 +- MessageThrottle.podspec | 2 +- MessageThrottle/MessageThrottle.m | 28 ++++++++++++++-------------- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/MTDemo/MTDemo/ViewController.m b/MTDemo/MTDemo/ViewController.m index 6193437..afb8fa5 100644 --- a/MTDemo/MTDemo/ViewController.m +++ b/MTDemo/MTDemo/ViewController.m @@ -35,7 +35,7 @@ - (void)viewDidLoad { // 跟上面的用法等价 [self.stub mt_limitSelector:@selector(foo:) oncePerDuration:0.5 usingMode:MTPerformModeDebounce onMessageQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)]; NSArray *rules = self.stub.mt_allRules; - self.stub = nil; +// self.stub = nil; for (MTRule *rule in rules) { NSLog(@"%@", rule); diff --git a/MessageThrottle.podspec b/MessageThrottle.podspec index 2d380c0..a2386f8 100644 --- a/MessageThrottle.podspec +++ b/MessageThrottle.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "MessageThrottle" -s.version = "1.1.1" +s.version = "1.1.2" s.summary = "A lightweight Objective-C message throttle and debounce library." s.description = <<-DESC MessageThrottle is a lightweight, simple library for controlling frequency of forwarding Objective-C messages. You can choose to control existing methods per instance or per class. It's an implementation of function throttle/debounce developed with Objective-C runtime. diff --git a/MessageThrottle/MessageThrottle.m b/MessageThrottle/MessageThrottle.m index 62d1ddb..26c52cc 100644 --- a/MessageThrottle/MessageThrottle.m +++ b/MessageThrottle/MessageThrottle.m @@ -141,14 +141,14 @@ - (BOOL)applyRule:(MTRule *)rule __block BOOL shouldApply = YES; if (mt_checkRuleValid(rule)) { [self.rules enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, MTRule * _Nonnull obj, BOOL * _Nonnull stop) { - if (sel_isEqual(rule.selector, obj.selector) - && mt_object_isClass(rule.target) - && mt_object_isClass(obj.target)) { - Class clsA = rule.target; - Class clsB = obj.target; + if (sel_isEqual(rule.selector, obj.selector)) { + + Class clsA = mt_classOfTarget(rule.target); + Class clsB = mt_classOfTarget(obj.target); + shouldApply = !([clsA isSubclassOfClass:clsB] || [clsB isSubclassOfClass:clsA]); *stop = shouldApply; - NSCAssert(NO, @"Error: %@ already apply rule in %@. A message can only have one rule per class hierarchy.", NSStringFromSelector(obj.selector), NSStringFromClass(clsB)); + NSCAssert(shouldApply, @"Error: %@ already apply rule in %@. A message can only have one rule per class hierarchy.", NSStringFromSelector(obj.selector), NSStringFromClass(clsB)); } }]; @@ -205,13 +205,7 @@ static BOOL mt_checkRuleValid(MTRule *rule) if ([selectorName isEqualToString:@"forwardInvocation:"]) { return NO; } - Class cls; - if (mt_object_isClass(rule.target)) { - cls = rule.target; - } - else { - cls = object_getClass(rule.target); - } + Class cls = mt_classOfTarget(rule.target); NSString *className = NSStringFromClass(cls); if ([className isEqualToString:@"MTRule"] || [className isEqualToString:@"MTEngine"]) { return NO; @@ -300,7 +294,7 @@ static void mt_forwardInvocation(__unsafe_unretained id assignSlf, SEL selector, static NSString *const MTForwardInvocationSelectorName = @"__mt_forwardInvocation:"; -static void mt_overrideMethod(id target, SEL selector) +static Class mt_classOfTarget(id target) { Class cls; if (mt_object_isClass(target)) { @@ -309,6 +303,12 @@ static void mt_overrideMethod(id target, SEL selector) else { cls = object_getClass(target); } + return cls; +} + +static void mt_overrideMethod(id target, SEL selector) +{ + Class cls = mt_classOfTarget(target); Method originMethod = class_getInstanceMethod(cls, selector); if (!originMethod) {