Last active
December 24, 2015 13:29
-
-
Save nsforge/6805167 to your computer and use it in GitHub Desktop.
KVC Helper Macros
This file contains hidden or 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
// 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