Skip to content

Instantly share code, notes, and snippets.

@nsforge
Last active December 24, 2015 13:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nsforge/6805167 to your computer and use it in GitHub Desktop.
Save nsforge/6805167 to your computer and use it in GitHub Desktop.
KVC Helper Macros
// KVC Compile-time Helpers
//
// These macros allow simple compile-time checking of KVC keypaths, as long as the getter methods for properties
// match up with their property names (i.e. if the property 'available' has the getter method 'isAvailable', these
// macros will break)
#define Key(class, key) (0 ? ((class *)nil).key, (NSString *)nil : @#key)
#define InstanceKey(instance, key) (0 ? ((__typeof(instance))nil).key, (NSString *)nil : @#key)
#define SelfKey(key) (0 ? ((__typeof(self))nil).key, (NSString *)nil : @#key)
#define ProtocolKey(protocol, key) (0 ? ((id <protocol>)nil).key, (NSString *)nil : @#key)
// Example usage, where the class Person has a property or method 'name'
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:Key(Person, name) ascending:YES];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment