La réponse courte est non, ou du moins pas à ce niveau. La réponse longue est qu'il est techniquement possible de construire quelque chose comme utiliser NSInvocations (et/ou forwardInvocation:
), faire quelque chose d'intelligent dans methodForSelector: et ou en enregistrant dynamiquement les implémentations de méthodes, mais c'est très compliqué, surtout si vous se soucient de la vitesse. Si j'avais un code où construire des méthodes curry comme ça en valait vraiment la peine, ce que je ferais, c'est quelque chose comme ça (écrit dans ce commentaire, non testé);
//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);
}
De toute évidence, en fonction de vos besoins que vous pourriez faire des choses quelque part différemment, par exemple, vous pourriez paresseusement par les garnements forwardInvocation ou les données Stash sur le sélecteur managled dans un dict dans l'instance au lieu de simplement managling dans le nom du sélecteur.
Après coup - je pourrais ajouter un KVP à la couche du bouton [helloButton.layer setValue: @ "hello" forKey: @ "param1"]; ... se référer au bouton quand il est passé à la fonction appelée ... mais c'est une solution assez moche! –