Skip to content

Commit

Permalink
NSInvocation
Browse files Browse the repository at this point in the history
  • Loading branch information
Lobster-King committed Apr 27, 2019
1 parent d2c7a16 commit 45d94d4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 140 deletions.
147 changes: 21 additions & 126 deletions SmartBlock/SmartBlock/NSObject+SmartBlock.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion SmartBlock/ThridView.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ - (void)drawRect:(CGRect)rect {

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
/*改变controller view的背景色,跨多个view回调*/
[self callBackUsingKey:@"touchCallBack",@"msg",nil];
[self callBackUsingKey:@"touchCallBack",@"msg1",@"msg2",nil];
}

@end
20 changes: 7 additions & 13 deletions SmartBlock/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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");

}

Expand Down

0 comments on commit 45d94d4

Please sign in to comment.