Skip to content

Instantly share code, notes, and snippets.

@zachwaugh
Last active July 29, 2016 12:39
Show Gist options
  • Save zachwaugh/10925757 to your computer and use it in GitHub Desktop.
Save zachwaugh/10925757 to your computer and use it in GitHub Desktop.
Experiment to figure out how NSDictionaryOfVariableBindings works, and try to make a more flexible version. This version strips underscores and supports self.* variables, but I'm sure there is a better way to do it.
#import <Foundation/Foundation.h>
// Experimental improvement to NSDictionaryOfVariableBindings where keys are simplified to remove underscores and "self." prefixes
// so you can use the simple version within the VFL string
//
// Example:
//
// [NSLayoutConstraint constraintsWithVisualFormat:@"|-[_foo]-[self.bar]-[baz]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_foo, self.bar, baz)];
// -> this doesn't work, gives an error about "self."
//
// [NSLayoutConstraint constraintsWithVisualFormat:@"|-[foo]-[bar]-[baz]" options:0 metrics:nil views:ZWDictionaryOfVariableBindings(_foo, self.bar, baz)];
#define ZWDictionaryOfVariableBindings(...) _ZWDictionaryOfVariableBindings(@"" #__VA_ARGS__, __VA_ARGS__, nil)
NSDictionary * _ZWDictionaryOfVariableBindings(NSString *commaSeparatedKeysString, id firstValue, ...) {
va_list args;
va_start(args, firstValue);
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
NSArray *keys = [commaSeparatedKeysString componentsSeparatedByString:@", "];
NSInteger idx = 0;
for (id arg = firstValue; arg != nil; arg = va_arg(args, id)) {
NSString *key = [[keys[idx] stringByReplacingOccurrencesOfString:@"_" withString:@""] stringByReplacingOccurrencesOfString:@"self." withString:@""];
NSLog(@"variable: %@, key: %@, value: %@", keys[idx], key, arg);
dict[key] = arg;
idx++;
}
va_end(args);
return dict;
}
int main(int argc, char *argv[]) {
NSString *_foo = @"__foo__";
NSString *bar_ = @"__bar__";
NSString *baz = @"__baz__";
NSDictionary *dict = ZWDictionaryOfVariableBindings(_foo, bar_, baz);
NSLog(@"dictionary: %@", dict);
}
// clang -framework Foundation ZWDictionaryOfVariableBindings.m -o test && ./test
// Output:
// 2014-04-16 15:48:58.686 ZWDictionaryOfVariableBindings[66046:507] variable: _foo, key: foo, value: __foo__
// 2014-04-16 15:48:58.688 ZWDictionaryOfVariableBindings[66046:507] variable: bar_, key: bar, value: __bar__
// 2014-04-16 15:48:58.688 ZWDictionaryOfVariableBindings[66046:507] variable: baz, key: baz, value: __baz__
// 2014-04-16 15:48:58.689 ZWDictionaryOfVariableBindings[66046:507] dictionary: {
// bar = "__bar__";
// baz = "__baz__";
// foo = "__foo__";
// }
@zachwaugh
Copy link
Author

This was a quick hack, I wouldn't actually use this without some more tests

@zachwaugh
Copy link
Author

For anyone curious, the macro/function setup is exactly how NSDictionaryOfVariableBindings works, so I just copied the same method:

// From NSLayoutConstraint.h

/* This macro is a helper for making view dictionaries for +constraintsWithVisualFormat:options:metrics:views:.  
 NSDictionaryOfVariableBindings(v1, v2, v3) is equivalent to [NSDictionary dictionaryWithObjectsAndKeys:v1, @"v1", v2, @"v2", v3, @"v3", nil];
 */
#define NSDictionaryOfVariableBindings(...) _NSDictionaryOfVariableBindings(@"" # __VA_ARGS__, __VA_ARGS__, nil)
UIKIT_EXTERN NSDictionary *_NSDictionaryOfVariableBindings(NSString *commaSeparatedKeysString, id firstValue, ...) NS_AVAILABLE_IOS(6_0); // not for direct use

@kaandedeoglu
Copy link

This is great, I didn't know #VAR_ARGS could get the inputs as strings like that! Thank you for sharing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment