有時候我們在使用函數中,函數內部的很多步驟是重復書寫的,同一個函數的某些環節的調用不同函數時,可以直接傳(SEL)作為參數,但當某個函數的參數是block時,可以使用下面的定義
sel:待執行方法
arguments:函數對應參數
- (void)invocationSEL:(SEL)sel arguments:(NSArray*)arguments{
? ? NSMethodSignature *signature = [[self class] instanceMethodSignatureForSelector:sel];
? ? NSInvocation*invocation? = [NSInvocationinvocationWithMethodSignature:signature];
? ? [invocationsetTarget:self];
? ? [invocationsetSelector:sel];
? ? for(inti =0; i< arguments.count; i++) {
? ? ? ? idargument = [argumentswim_objAtIndex:i];
? ? ? ? [invocationsetArgument:&argumentatIndex:1+i];
? ? }
? ? [invocationinvoke];
}
eg:
typedef?void(^handlerBlock)(NSArray*__nullable?array,NSError*__nullable?error);
handlerBlock handler = ^(NSArray*__nullable?array,NSError*__nullable?error){
? ? ? ? ? ? ? ? ? ? };
[self invocationSEL:sel arguments:@[array,handler]];