Skip to content

Instantly share code, notes, and snippets.

@quinntaylor
Created November 13, 2014 06:14
Show Gist options
  • Save quinntaylor/d3f124dd4f812d6cc6de to your computer and use it in GitHub Desktop.
Save quinntaylor/d3f124dd4f812d6cc6de to your computer and use it in GitHub Desktop.
Code for getting compiler check on key paths, more accurate than using NSStringFromSelector(@selector(foo)).
// See http://nshipster.com/key-value-observing/
// Put this code a common utilities header, and use it to have the compiler help check correctness of key paths.
// Uses macro stringification to create an Obj-C string literal, plus validation code that the compiler optimizes out.
@interface NSObject (KeyPathFakeCategoryForCompilerWarnings)
+ (instancetype)__fake_method_for_compiler_warnings__;
- (instancetype)__fake_method_for_compiler_warnings__;
@end
/*! Returns a string for the given keypath, but causes a compiler warning if the keypath is not defined on \c self.
\note Works for both instance and class methods.
*/
#define KeyPathForSelf(__keypath) \
({if (NO) {(void)[super __fake_method_for_compiler_warnings__].__keypath;} @#__keypath;})
/*! Returns a string for the given keypath, but causes a compiler warning if the keypath is not defined on \a __object.
*/
#define KeyPathForObject(__object, __keypath) \
({if (NO) {(void)__object.__keypath;} @#__keypath;})
/*! Returns a string for the given keypath, but causes a compiler warning if the keypath is not defined on \a __class.
*/
#define KeyPathForClass(__class, __keypath) \
({if (NO) {__class *__object = nil; (void)__object.__keypath;} @#__keypath;})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment