objectMatcher
devulder
Membre
Hello,
Voila une classe assez sympa pour faire des switch sur des objets,selector ou string !
Exemple:
objswitch(someObject)
objcase(@one)
{
// Nesting works.
objswitch(@b)
objcase(@a) printf("one/a");
objcase(@b) printf("one/b");
endswitch
// Any code can go here, including break/continue/return.
}
objcase(@two) printf("It's TWO."); // Can omit braces.
objcase(@three, // Can have multiple values in one case.
nil, // nil can be a "case" value.
[self self], // "Case" values don't have to be constants.
@tres,
@trois) { printf("It's a THREE."); }
defaultcase printf("None of the values above."); // Optional default must be at end.
endswitch
objswitch(someObject)
objkind(NSNumber) { printf("It's a NUMBER."); }
objkind(NSString) { printf("It's a STRING."); }
objkind([NSArray class],
[NSDictionary class],
[NSSet class]) printf("It's a collection.");
endswitch
selswitch([anItem action])
selcase(@selector(selectSuperclass)
{
// ...
}
selcase(@selector(selectAncestorClass)
{
// ...
}
selcase(@selector(selectFormalProtocolsTopic,
@selector(selectInformalProtocolsTopic,
@selector(selectFunctionsTopic)
{
// ...
}
endswitch
for (id ifNumericWhatIsIt in @[;@99, @0, @shnitzel])
objswitch(ifNumericWhatIsIt)
objkind(NSNumber) printf("It's a NUMBER.... ");
objswitch([ifNumericWhatIsIt stringValue])
objcase(@3) printf("It's THREE.\n");
objcase(@99) printf("It's NINETY-NINE.\n");
defaultcase printf("some other Number.\n");
endswitch
defaultcase printf("It's something else entirely.\n");
endswitch
C'est sur GitHub https://github.com/aglee/ObjectMatcher
Connectez-vous ou Inscrivez-vous pour répondre.
Réponses
Cela me rappelle un speech CocoaHeads Paris où un gars s'était amusé à faire la même chose.
Des switchs sur des NSString (et plus).
https://github.com/AliSoftware/ObjcSwitch Je ne sais pas si ça fait la même chose
[EDIT] Bon bah le temps que je poste samir m'a déjà devancé en citant mon projet...
---
Sauf que depuis, maintenant qu'on a les object literals, c'est à se demander si c'est toujours nécessaire, vu qu'on peut profiter de cette syntaxe pour écrire des choses comme ça au final...
çà fait quoi de se faire griller AliGator ?? ;D (pour une fois !)