Skip to content

Instantly share code, notes, and snippets.

@tgaul
Forked from kyleve/gist:8213806
Last active August 29, 2015 13:56
Show Gist options
  • Save tgaul/9156499 to your computer and use it in GitHub Desktop.
Save tgaul/9156499 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)
/**
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
Example:
+ (NSSet *)keyPathsForValuesAffectingDependentKey
{
return [NSSet setWithObjects: SQClassKeyPath(dependedUponKey1),
SQClassKeyPath(dependedUponKey2),
nil];
}
*/
#define SQClassKeyPath(keyPath) SQTypedKeyPath(SQKeyPath_CLASS, keyPath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment