Re: NSArray KVC proxy
Re: NSArray KVC proxy
- Subject: Re: NSArray KVC proxy
- From: Jeff Johnson <email@hidden>
- Date: Sun, 25 Jan 2009 15:20:29 -0600
On Jan 25, 2009, at 2:54 PM, Quincey Morris wrote:
Consider a class with an array property 'xxxx' implemented KVC-
compliantly by the following methods:
- (NSUInteger) countOfXxxx;
- (id) objectInXxxxAtIndex: (NSUInteger) index;
(Assume there's no NSArray instance variable backing this property.)
So, users of the class can call [valueForKey:@"xxxx"] to get a proxy
object that returns the objects in the "array".
Now, the question is: How do I implement a getter? The intention is
that it's just a convenience method for getting a proxy:
@property (readonly) (NSArray*) xxxx;
- (NSArray*) xxxx {
return [self valueForKey: @"xxxx"];
}
but that is a *really* bad idea, because valueForKey: will *first*
try calling the 'xxxx' method -- the one it was called from.
As I missing something simple here? It seems like there needs to be
an 'arrayValueForKey:' method (parallel to
mutableArrayValueForKey:), which checks whether the class implements
the indexed accessors before calling the getter, instead of after.
If you want to implement the getter xxxx, then you don't need to
implement countOfXxxx and objectInXxxxAtIndex:. Since your backing
store is not an array, the implementation of the getter should create
and return an array. In the docs for KVC compliance, it says that you
should implement the getter OR implement the indexed accessors.
http://developer.apple.com/documentation/Cocoa/Conceptual/KeyValueCoding/Concepts/Compliant.html
The question of whether it's preferable to implement the getter or the
indexed accessors really depends on how the property is used in your
app.
-Jeff
_______________________________________________
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