Skip to content

Instantly share code, notes, and snippets.

@jspahrsummers
Created September 25, 2012 13:26
Show Gist options
  • Save jspahrsummers/3780283 to your computer and use it in GitHub Desktop.
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
// 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