Last active
June 21, 2018 09:43
-
-
Save holtwick/27321fa97231fcba3ea3fb8621517865 to your computer and use it in GitHub Desktop.
Autocomplete and refactoring compatible keyPaths: https://holtwick.de/blog/keypath-refatoring
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
// (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