ios 函數嵌套名稱不同的內置函數

有時候我們在使用函數中,函數內部的很多步驟是重復書寫的,同一個函數的某些環節的調用不同函數時,可以直接傳(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]];

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容