Skip to content

Instantly share code, notes, and snippets.

@kyleve
Created January 2, 2014 01:46
Show Gist options
  • Save kyleve/8213806 to your computer and use it in GitHub Desktop.
Save kyleve/8213806 to your computer and use it in GitHub Desktop.
/**
Provides the ability to verify key paths at compile time.
If "keyPath" does not exist, a compile-time error will be generated.
Example:
// Verifies "isFinished" exists on "operation".
NSString *key = SQKeyPath(operation, isFinished);
// Verifies "isFinished" exists on self.
NSString *key = SQSelfKeyPath(isFinished);
// Verifies "isFinished" exists on instances of NSOperation.
NSString *key = SQTypedKeyPath(NSOperation, isFinished);
*/
#define SQKeyPath(object, keyPath) ({ if (NO) { (void)((object).keyPath); } @#keyPath; })
#define SQSelfKeyPath(keyPath) SQKeyPath(self, keyPath)
#define SQTypedKeyPath(ObjectClass, keyPath) SQKeyPath(((ObjectClass *)nil), keyPath)
#define SQProtocolKeyPath(Protocol, keyPath) SQKeyPath(((id <Protocol>)nil), keyPath)
@hfossli
Copy link

hfossli commented Jan 5, 2014

Whic is better? This or
https://github.com/ReactiveCocoa/ReactiveCocoa/blob/master/ReactiveCocoaFramework/ReactiveCocoa/extobjc/EXTKeyPathCoding.h ?

This gist has no requirements, I figure that's a pro.

@ndonald2
Copy link

ndonald2 commented Jan 9, 2014

Maybe worth noting that this will not validate keys backed only by ivars or keys not backed by anything and handled only by implementing valueForUndefinedKey:, although neither of those cases are very common.

@tgaul
Copy link

tgaul commented Feb 22, 2014

One addition we made to this in our project is to allow easier use in class methods (such as when defining the keyPathsForValuesAffecting... methods for KVO, for example) without having to repeat the class name each time:

#define SQClassKeyPath(keyPath) SQTypedKeyPath(SQKeyPath_CLASS, keyPath)

To use SQClassKeyPath, your implementation (.m) file should have a line like this (I usually put it after the @implementation in question):

typedef MyClass ISFKeyPath_CLASS;

Or if you have multiple classes defined in one .m file, you can use a macro, which you can undef.

#undef ISFKeyPath_CLASS
#define ISFKeyPath_CLASS MyClass

As a result, you can now define class methods that reference key paths of the class like so:

+ (NSSet *)keyPathsForValuesAffectingDependentKey
{
    return [NSSet setWithObjects: SQClassKeyPath(dependedUponKey1), 
                                  SQClassKeyPath(dependedUponKey2), 
                                  nil];
}

@seivan
Copy link

seivan commented Jul 21, 2014

Thank you!

@xavierjurado
Copy link

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