diff --git a/SmartBlock/SmartBlock/NSObject+SmartBlock.m b/SmartBlock/SmartBlock/NSObject+SmartBlock.m index 361e929..258a4eb 100644 --- a/SmartBlock/SmartBlock/NSObject+SmartBlock.m +++ b/SmartBlock/SmartBlock/NSObject+SmartBlock.m @@ -222,74 +222,21 @@ - (void)invokeBlockOnDefaultThreadWithInfo:(BlockInfo *)blockInfo { NSMethodSignature *blockSignature = [NSMethodSignature signatureWithObjCTypes:__BlockSignature__(block)]; if (argumentsRef.count != ([blockSignature numberOfArguments] - 1)) { - // NSAssert(0, @"参数个数不符!"); /*参数个数不符合,直接丢弃本次block调用*/ NSLog(@"Smart_Block_参数个数不符!"); return; } - /* - 1.不做参数具体类型校验 - 2.上层业务负责类型校验 - */ - switch (argumentsRef.count) { - case 0: - { - void (^blockRef)(void) = block; - blockRef(); - } - break; - case 1: - { - void (^blockRef)(id) = block; - blockRef(args[0]); - } - break; - case 2: - { - void (^blockRef)(id,id) = block; - blockRef(args[0],args[1]); - } - break; - case 3: - { - void (^blockRef)(id,id,id) = block; - blockRef(args[0],args[1],args[2]); - } - break; - case 4: - { - void (^blockRef)(id,id,id,id) = block; - blockRef(args[0],args[1],args[2],args[3]); - } - break; - case 5: - { - void (^blockRef)(id,id,id,id,id) = block; - blockRef(args[0],args[1],args[2],args[3],args[4]); - } - break; - case 6: - { - void (^blockRef)(id,id,id,id,id,id) = block; - blockRef(args[0],args[1],args[2],args[3],args[4],args[5]); - } - break; - case 7: - { - void (^blockRef)(id,id,id,id,id,id,id) = block; - blockRef(args[0],args[1],args[2],args[3],args[4],args[5],args[6]); - } - break; - case 8: - { - void (^blockRef)(id,id,id,id,id,id,id,id) = block; - blockRef(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7]); - } - break; - - default: - break; + + NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:blockSignature]; + NSInteger index = 1; + for (id arg in args) { +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wincompatible-pointer-types-discards-qualifiers" + [invocation setArgument:&arg atIndex:index]; +#pragma clang diagnostic pop + index++; } + [invocation invokeWithTarget:block]; } - (void)invokeBlockOnObserverThreadWithInfo:(BlockInfo *)blockInfo { @@ -298,74 +245,22 @@ - (void)invokeBlockOnObserverThreadWithInfo:(BlockInfo *)blockInfo { NSMethodSignature *blockSignature = [NSMethodSignature signatureWithObjCTypes:__BlockSignature__(block)]; if (argumentsRef.count != ([blockSignature numberOfArguments] - 1)) { - // NSAssert(0, @"参数个数不符!"); /*参数个数不符合,直接丢弃本次block调用*/ NSLog(@"Smart_Block_参数个数不符!"); return; } - /* - 1.不做参数具体类型校验 - 2.上层业务负责类型校验 - */ - switch (argumentsRef.count) { - case 0: - { - void (^blockRef)(void) = block; - blockRef(); - } - break; - case 1: - { - void (^blockRef)(id) = block; - blockRef(args[0]); - } - break; - case 2: - { - void (^blockRef)(id,id) = block; - blockRef(args[0],args[1]); - } - break; - case 3: - { - void (^blockRef)(id,id,id) = block; - blockRef(args[0],args[1],args[2]); - } - break; - case 4: - { - void (^blockRef)(id,id,id,id) = block; - blockRef(args[0],args[1],args[2],args[3]); - } - break; - case 5: - { - void (^blockRef)(id,id,id,id,id) = block; - blockRef(args[0],args[1],args[2],args[3],args[4]); - } - break; - case 6: - { - void (^blockRef)(id,id,id,id,id,id) = block; - blockRef(args[0],args[1],args[2],args[3],args[4],args[5]); - } - break; - case 7: - { - void (^blockRef)(id,id,id,id,id,id,id) = block; - blockRef(args[0],args[1],args[2],args[3],args[4],args[5],args[6]); - } - break; - case 8: - { - void (^blockRef)(id,id,id,id,id,id,id,id) = block; - blockRef(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7]); - } - break; - - default: - break; + + NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:blockSignature]; + NSInteger index = 1; + for (id arg in args) { +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wincompatible-pointer-types-discards-qualifiers" + [invocation setArgument:&arg atIndex:index]; +#pragma clang diagnostic pop + index++; } + [invocation invokeWithTarget:block]; + blockInfo.finished = YES; } diff --git a/SmartBlock/ThridView.m b/SmartBlock/ThridView.m index 8991d9a..976addb 100644 --- a/SmartBlock/ThridView.m +++ b/SmartBlock/ThridView.m @@ -40,7 +40,7 @@ - (void)drawRect:(CGRect)rect { - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { /*改变controller view的背景色,跨多个view回调*/ - [self callBackUsingKey:@"touchCallBack",@"msg",nil]; + [self callBackUsingKey:@"touchCallBack",@"msg1",@"msg2",nil]; } @end diff --git a/SmartBlock/ViewController.m b/SmartBlock/ViewController.m index d6c4c55..19f368c 100644 --- a/SmartBlock/ViewController.m +++ b/SmartBlock/ViewController.m @@ -25,25 +25,19 @@ - (void)viewDidLoad { self.baseView.center = self.view.center; [self.view addSubview:self.baseView]; - [self.baseView removeFromSuperview]; - self.baseView = nil; - - [self callBackUsingKey:@"BaseViewCallBack", nil]; - NSString *address = [NSString stringWithFormat:@"%p",self.baseView]; NSString *retrievedObject; sscanf([address cStringUsingEncoding:NSUTF8StringEncoding], "%p", &retrievedObject); -// __weak typeof(self)weakSelf = self; -// [self observeCallBackUsingKey:@"touchCallBack" callBack:^(NSString *msg) { -// NSLog(@"%s",__func__); -// weakSelf.view.backgroundColor = [UIColor orangeColor]; -// } destructionOption:BlockDestructionDefault]; + __weak typeof(self)weakSelf = self; + [self observeCallBackUsingKey:@"touchCallBack" callBack:^(NSString *msg,NSString *second) { + NSLog(@"%s",__func__); + weakSelf.view.backgroundColor = [UIColor orangeColor]; + } destructionOption:BlockDestructionDefault]; // - [NSThread detachNewThreadSelector:@selector(addObserver) toTarget:self withObject:nil]; - NSLog(@"子线程中注册Block"); - // Do any additional setup after loading the view, typically from a nib. +// [NSThread detachNewThreadSelector:@selector(addObserver) toTarget:self withObject:nil]; +// NSLog(@"子线程中注册Block"); }