If you want some slightly smarter dispatch than a long list of conditionals you can use a dictionary of blocks:
NSString *key = @"foo";
void (^selectedCase)() = @{
@"foo" : ^{
NSLog(@"foo");
},
@"bar" : ^{
NSLog(@"bar");
},
@"baz" : ^{
NSLog(@"baz");
},
}[key];
if (selectedCase != nil)
selectedCase();
If you have a really long list of cases and you do this often, there might be a tiny performance advantage in this. You should cache the dictionary, then (and don't forget to copy the blocks).
Sacrificing legibility for convenience and brevity here's a version that fits everything into a single statement and adds a default case:
((void (^)())@{
@"foo" : ^{
NSLog(@"foo");
},
@"bar" : ^{
NSLog(@"bar");
},
@"baz" : ^{
NSLog(@"baz");
},
}[key] ?: ^{
NSLog(@"default");
})();
I prefer the former.