Op 12-11-2010 2:26, Ken Thomases schreef:
On Nov 11, 2010, at 4:57 PM, Graham Cox wrote:

On 12/11/2010, at 3:30 AM, Ken Thomases wrote:

You should not override -setValue:forKey: or -valueForKey: if you can avoid it.  Instead, 
implement the methods -setValue:forUndefinedKey: and -valueForUndefinedKey:.  They are 
precisely for implementing "dynamic" properties in this manner.

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.

He was using an NSMutableDictionary for his model, instead of a custom class.  
The barrier to using a custom class was that he has many properties and didn't 
want to implement them all.  The suggestion was to make a class which wraps a 
mutable dictionary and use the KVC methods to provide access to them and also 
serve as the trigger for retrieving those which are not yet cached.

All fine so far.

The suggestion, though, was to implement the wrapper by overriding -setValue:forKey: and 
-valueForKey:.  You shouldn't override those, but rather override 
-setValue:forUndefinedKey: and -valueForUndefinedKey:.  The respective implementations 
would be the same -- they would pass through to the mutable dictionary and initiate 
fetches for absent values.  -setValue:forKey: and -valueForKey: have some "special 
powers" that you lose if you override them.

I think I don't get it. I now have to following:
@interface PropertiesController: NSObject {
        NSMutableDictionary *properties;
}

-valueForKey:
-setValue:forKey:
@end
@implementation PropertiesController
valueForKey  {
        id retVal=[properties valueForKey:key];
        if (!retVal) {
                //fetch value from network
                //We do not wait for the value
        }
        return retVal;
}
@end
If I ommit the valueForKey method, how does the KVC logic now, that it should check the properties variable? Further more, NSDictionary seems to always return a value, so all keys are "defined", they just return nil sometimes. How can valueForUndefinedKey: then ever be called?

Kind regards,

Remco Poelstra
_______________________________________________

Cocoa-dev mailing list ([email protected])

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to