Skip to content

Instantly share code, notes, and snippets.

@holtwick holtwick/KeyPathHelper.m

Last active Jun 21, 2018
Embed
What would you like to do?
Autocomplete and refactoring compatible keyPaths: https://holtwick.de/blog/keypath-refatoring
// (C)opyright 2018-06-20 Dirk Holtwick, holtwick.it. All rights reserved.
#import <Foundation/Foundation.h>
#define keyPath(k) YES ? @#k : (k ? @"": nil)
#define keyPathFromObject(o, k) YES ? @#k : ((o ?: o.k) ? @"" : nil)
@interface Sample : NSObject
@property id greeting;
@end
@implementation Sample
@end
@interface Demo : NSObject
@property Sample *sample;
@end
@implementation Demo
- (instancetype)init {
self = [super init];
if (self) {
self.sample = [[Sample alloc] init];
self.sample.greeting = @"Hello World";
id akp = keyPath(self.sample.greeting);
id a = [self valueForKeyPath:akp];
NSLog(@"Result: %@ = %@", akp, a);
id bkp = keyPathFromObject(self.sample, greeting);
id b = [self.sample valueForKeyPath:bkp];
NSLog(@"Result: %@ = %@", bkp, a);
NSAssert([a isEqualToString:b], @"Same");
}
return self;
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
[[Demo alloc] init];
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.