Skip to content

Instantly share code, notes, and snippets.

@emreberge
Last active September 3, 2015 12:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emreberge/9c38488284c9c86b349a to your computer and use it in GitHub Desktop.
Save emreberge/9c38488284c9c86b349a to your computer and use it in GitHub Desktop.
Compiler help for keyPaths!
#define KeyPath(keyPath)\
^NSString *(void) {\
__unused __typeof__(keyPath) x;\
return [@#keyPath substringFromIndex:([@#keyPath rangeOfString:@"."].location + 1)];\
}()
@emreberge
Copy link
Author

screen shot 2015-07-27 at 21 58 08

@akull
Copy link

akull commented Aug 19, 2015

I often find that you want to observe an object that is already a property of another object, and then this kind of breaks down. For example, this snippet does not work:

[self.object addObserver:self forKeyPath:KeyPath(self.object.myCoolKeyPath) options:kNilOptions context:NULL]

To support this case as well, we can add the NSBackwardsSearch option to the rangeOfString call.

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