key-value paths and NSArray
key-value paths and NSArray
- Subject: key-value paths and NSArray
- From: Christopher Corbell <email@hidden>
- Date: Sat, 20 Dec 2003 22:34:17 -0800
I'm working on a document model that uses a dictionary with
property list serialization. As expected there are two ways that
object can be nested in the document schema, as contents of
dictionaries or as contents of arrays. The schema is dynamic -
it can contain arbitrarily nested values. I'm trying to fully support
the key-value coding mechanisms, but there's one point I don't
quite get regarding NSArray and the valueForKey method.
What I want to be able to do is have a key-value path capable of
uniquely identifying any object in the document. If I understand
the new behavior of [NSArray valueForKey], this isn't possible in
any generic way, because NSArray will interpret a key as something
to pass off to its sub-objects. From the release notes it sounds like
you could have formerly used something like @"objectAtIndex:2"
as a key for a specific item in an array, but this is no longer the
case.
This means that the best a key-value path can do in a generic
fashion is to return the array (if it's the value for a dictionary key).
It can't return a specific object in the array at all. It can return
the
object contained by (or key-value encoded by) an object contained
in the array, but only if all objects in the array support key-value
coding (NSNumber is out) and even then it will return -all- the
values matching the key, so if you have an array of identically
schemed dictionaries you'll still have to select one from an array
of results.
By completely taking over the valueForKeyPath implementation
for my document I've figured out a way to make the unique-key-path
to-object requirement work, but I'm wondering if there isn't some more
standard mechanism?
My approach is to check at runtime the target for each key-path
segment; if it's a dictionary I call valueForKey, and if it's an array
I assume the path-segment to be an index, i.e. a string whose
-intValue method will return the index of the desired item. Maybe
I should rename the dictionary's method -uniqueValueForKeyPath
or -valueForKeyOrIndexPath or something like that.
However I'm a little concerned that as key-value coding is a protocol
which is gaining ground across AppKit APIs (the controller layer,
AppleScript, etc.) doing this my own way will mean sacrificing things
that
I might otherwise get for free at present or in the future.
Thanks for any advice on these issues.
- Chris
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.