簡短答案是否定的,或者至少不是在那個級別。
長的答案是,在技術上可以構建類似於使用NSInvocations(和/或forwardInvocation:
)的東西,在methodForSelector中做一些聰明的事情,或者通過動態註冊方法實現,但這非常棘手,特別是如果你關心速度。
如果我有一些代碼來建立這樣的curry方法真的很值得,我會做的就是這樣(寫在這個評論中,未經測試);
//FIXME: In a real implementation you would do some mangling, this code will get confused if you have _s in the curried selector, and thus could be exploitable
//This method makes a unique selector by mangling the arguments
- (SEL) selectorForSelector:(SEL)bindSel withString:(NSString *)bindString {
NSString *mangle = [NSString *stringWithFormat:@"LGBind_%@_%@"], NSStringFromSelector(bindSel), bindString];
SEL retval = NSSelectorFromString(mangle);
//Register the imp. You probably want to check if it is already reg
if (![self respondsToSelector:retval]) {
class_addMethod([self class], retval, LGBind_IMP, "[email protected]:")l
}
}
//Generic dispatcher imp
void LGBind_IMP(id self, SEL _cmd) {
NSString *selectorName = NSStringFromSelector(_cmd);
NSArray *array [selectorName componentsSeparatedByString:@"_"];
//Skip index 0; it is @"LGBind"
NSString *originalSelectorString = [array objectAtIndex:1];
NSString *originalArgString = [array objectAtIndex:2];
//Get our the SEL and the IMP
SEL originalSEL = NSSelectorFromString(originalSelectorString);
IMP originalIMP = [self methodForSelector:originalSEL];
//call the original imp
originalIMP([self class], originalSEL, originalArgString);
}
顯然,這取決於你可以做不同的事情的地方您的具體需求,比如你可以懶洋洋地在forwardInvocation的小鬼,或約在實例字典的managled選擇藏匿的數據,而不是僅僅managling成選擇器名稱。
事後考慮 - 我可以添加一個KVP到按鈕層 [helloButton.layer setValue:@「hello」forKey:@「param1」]; ...然後參考按鈕,因爲它傳遞給被調用的函數......但這是一個非常醜陋的解決方案! – 2009-11-11 11:48:51