|
|
|
|
|
by Zev
4841 days ago
|
|
You can easily do without a protocol[1] -- NSKeyValueCoding doesn't require a protocol to be implemented anywhere. For example: @interface MYObject : NSObject
@property (nonatomic, copy) NSString *foo;
@property (nonatomic, copy) NSString *bar;
@end
@implementation MYObject
@end
And somewhere else in your code: - (MYObject *) buildMYObject {
MYObject *object = [[MYObject alloc] init];
[object setValuesForKeysWithDictionary:@{ @"foo": @"purple", @"bar": @"elephant" }];
return object;
}
Then you can access it like a regular property: - (void) useMYObject {
MYObject *builtObject = [self buildMYObject];
NSLog(@"value: %@", builtObject.foo); // will print "value: purple"
NSLog(@"value: %@", builtObject.bar); // will print "value: elephant"
}
There are some gotchas to be aware of here (that Parse likely takes care of, since you're subclassing PFObject), such as setting nil values, or attempting to set the value from a key that doesn't have a corresponding property on your object.1. It looks like the main benefit to the protocol is compatibility with the existing design of -[PFObject objectWithClassName:], and, I don't know enough about Parse to say what that does. But, I bet its nifty. |
|