Skip to content

Instantly share code, notes, and snippets.

@Kam-To
Last active April 11, 2021 09:11
Show Gist options
  • Save Kam-To/a8a16dd026a06a9683d7ded180730f4a to your computer and use it in GitHub Desktop.
Save Kam-To/a8a16dd026a06a9683d7ded180730f4a to your computer and use it in GitHub Desktop.
handy marco for key-value binds support nil value in value list
#define KTOptionalBindings(...) __KTOptionalBindings(@#__VA_ARGS__,__VA_ARGS__)
NSMutableDictionary *__KTOptionalBindings(NSString *command_value_list, id firstValue, ...) {
va_list args;
va_start(args, firstValue);
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
NSArray *keys = [command_value_list componentsSeparatedByString:@", "];
NSInteger inputCount = keys.count;
for (NSInteger i = 0; i < inputCount; i++) {
NSString *key = keys[i];
id val = nil;
if (i == 0) {
val = firstValue;
} else {
val = va_arg(args, id);
}
dict[key] = val;
}
va_end(args);
return dict;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment