KVC to-many relationships
KVC to-many relationships
- Subject: KVC to-many relationships
- From: Jesse Grosjean <email@hidden>
- Date: Tue, 4 Nov 2003 13:01:56 -0500
I'm trying to use key value coding with with a to-many relationship but
seem to be running into some naming issues. I have a node and it has an
array of parents and an array of children. My goal is to support
mutable key value coding for these two relationships and I don't want
to commit to a NSMutableArray representation so I would like to "hide"
behind the
- countOfKey
- objectInKeyAtIndex:
- insertObject:inKeyAtIndex:
- removeObjectAtIndex:
methods.
For my first try I declared a NSMutableArray named "parents" and one
named "children". For that case my test method below works fine, but
the arrays seem to be accessed directly. None of the key value coding
methods ever get called. This seems like incorrect behavior to me, i
would expect the key value coding mechanism to first try to use the
accessor methods before directly accessing the underlying arrays.
The next thing that I've done is try to hide the internal arrays by
renaming them (as in the below example) but this results in a crash and
none of the key value coding works.
Last of all if i change the code to refer to "owners" and "subnodes"
everything works fine using the key value coding methods as expected.
Can anyone explain this behavior? Is there something special about the
"parents" and "children" keys?
Thanks,
Jesse
@implementation Node
+ (void)test {
Node *node = [[Node alloc] init];
Node *nodeParent = [[Node alloc] init];
Node *nodeChild = [[Node alloc] init];
NSMutableArray *parentsArray = [node
mutableArrayValueForKey:@"parents"];
NSMutableArray *childrenArray = [node
mutableArrayValueForKey:@"children"];
[parentsArray addObject:nodeParent];
[childrenArray addObject:nodeChild];
}
- (id)init {
if (self = [super init]) {
cantFindMeParents = [[NSMutableArray alloc] init];
cantFindMeChildren = [[NSMutableArray alloc] init];
}
return self;
}
- (void)dealloc {
[cantFindMeParents release];
[cantFindMeChildren release];
[super dealloc];
}
- (unsigned)countOfParents {
NSLog("countOfParents");
return [cantFindMeParents count];
}
- (id)objectInParentsAtIndex:(unsigned)index {
NSLog("objectInParentsAtIndex");
return [cantFindMeParents objectAtIndex:index];
}
- (void)insertObject:(id)object inParentsAtIndex:(unsigned)index {
NSLog("insertObjectinParentsAtIndex");
[cantFindMeParents insertObject:object atIndex:index];
}
- (void)removeObjectInParentsAtIndex:(unsigned)index {
NSLog("removeObjectInParentsAtIndex");
[cantFindMeParents removeObjectAtIndex:index];
}
- (unsigned)countOfChildren {
NSLog("countOfChildren");
return [cantFindMeChildren count];
}
- (id)objectInChildrenAtIndex:(unsigned)index {
NSLog("objectInChildrenAtIndex");
return [cantFindMeChildren objectAtIndex:index];
}
- (void)insertObject:(id)object inChildrenAtIndex:(unsigned)index {
NSLog("insertObjectinChildrenAtIndex");
[cantFindMeChildren insertObject:object atIndex:index];
}
- (void)removeObjectInChildrenAtIndex:(unsigned)index {
NSLog("removeObjectInChildrenAtIndex");
[cantFindMeChildren removeObjectAtIndex:index];
}
@end
_______________________________________________
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.