Created
January 2, 2014 01:46
-
Star
(165)
You must be signed in to star a gist -
Fork
(7)
You must be signed in to fork a gist
-
-
Save kyleve/8213806 to your computer and use it in GitHub Desktop.
This file contains 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
/** | |
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
To use SQClassKeyPath, your implementation (.m) file should have a line like this (I usually put it after the @implementation in question):
Or if you have multiple classes defined in one .m file, you can use a macro, which you can undef.
As a result, you can now define class methods that reference key paths of the class like so: