Created
September 25, 2012 13:26
-
-
Save jspahrsummers/3780283 to your computer and use it in GitHub Desktop.
Figuring out the object that a non-string, compiler-verified key path refers to
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// doesn't work with __block variables | |
#define keypath2(PATH) \ | |
do { \ | |
const char *_path = strchr(# PATH, '.') + 1; \ | |
\ | |
id _capturingBlock = ^{ \ | |
return PATH; \ | |
}; \ | |
\ | |
struct _literal { \ | |
void *isa; \ | |
int flags; \ | |
int reserved; \ | |
void (*invoke)(void *); \ | |
struct { \ | |
unsigned long reserved; \ | |
unsigned long blockSize; \ | |
} *descriptor; \ | |
const __unsafe_unretained id obj; \ | |
} *_literal = (__bridge struct _literal *)_capturingBlock; \ | |
\ | |
id _obj = _literal->obj; \ | |
NSLog(@"obj: %@ (key path \"%s\")", _obj, _path); \ | |
} while (0) | |
// Usage | |
keypath2(URL.port); | |
keypath2(URL.port.stringValue); | |
keypath2(obj.someUniqueProperty); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment