Re: Detecting reading a key in KVC
Re: Detecting reading a key in KVC
- Subject: Re: Detecting reading a key in KVC
- From: Kyle Sluder <email@hidden>
- Date: Thu, 11 Nov 2010 15:29:48 -0800
On Thu, Nov 11, 2010 at 2:57 PM, Graham Cox <email@hidden> wrote:
> Understood, but the OP's problem as I understand it is that it's not that the key is undefined, but the value associated with it is uninitialized. So rather than return nil, or zero, he wants to trigger a remote fetch of the value. KVC doesn't appear to provide a mechanism for that out of the box.
Nope, it doesn't. We have a very similar need, and solved it using a
custom protocol on our model objects. Now, in places where we are
about to demand an initialized value, we check to see if the model
object conforms to the protocol, and if so call the method that
switches from the uninitialized placeholder "zero" value to the
fully-fleshed object value.
// Typed in compose window, probably doesn't make sense.
@protocol KVOPlaceholder
- (void)needValueForKey:(NSString *)aKey;
@end
- (void)customerOrdered:(CatalogItem *)anItem {
// Because "0" is a valid quantity, this CatalogItem subclass might
need to be told that
// it can initialize its backing store for the "quantity" key now
if ([anItem conformsToProtocol:KVOPlaceholder])
[anItem needValueForKey:@"quantity"];
NSNumber *quantity = [anItem valueForKey:@"quantity"];
quantity = [NSNumber numberWithInteger:[quantity integerValue] - 1];
[anItem setValue:quantity forKey:@"quantity"];
}
--Kyle Sluder
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden