Skip to content

Commit

Permalink
Improved KVC mutable indexed accessors
Browse files Browse the repository at this point in the history
Refactored everything away into insertObjects:atIndexes: so all the logic is in the same place
and made it so that the _insertManySEL is the preferred selector to use. If it doesn't exist it will fall back to the _insertSEL.
  • Loading branch information
klaaspieter committed Aug 25, 2010
1 parent c975219 commit b21eab1
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions Foundation/CPArray+KVO.j
Expand Up @@ -120,7 +120,7 @@

_insertManySEL = sel_getName(@"insertObjects:in"+capitalizedKey+"AtIndexes:");
if ([_proxyObject respondsToSelector:_insertManySEL])
_insert = [_proxyObject methodForSelector:_insertManySEL];
_insertMany = [_proxyObject methodForSelector:_insertManySEL];

This comment has been minimized.

Copy link
@appden

appden Sep 9, 2010

Contributor

How come _removeMany and _replaceMany weren't fixed?

This comment has been minimized.

Copy link
@klaaspieter

klaaspieter Sep 10, 2010

Author Contributor

No particular reason. Feel free to submit a patch.


_removeManySEL = sel_getName(@"removeObjectsFrom"+capitalizedKey+"AtIndexes:");
if ([_proxyObject respondsToSelector:_removeManySEL])
Expand Down Expand Up @@ -234,13 +234,7 @@

- (void)addObject:(id)anObject
{
if (_insert)
return _insert(_proxyObject, _insertSEL, anObject, [self count]);

var target = [[self _representedObject] copy];

[target addObject:anObject];
[self _setRepresentedObject:target];
[self insertObject:anObject atIndex:[self count]];
}

- (void)addObjectsFromArray:(CPArray)anArray
Expand All @@ -249,18 +243,38 @@
count = [anArray count];

for (; index < count; ++index)
[self addObject:[anArray objectAtIndex:index]];
[self insertObject:[anArray objectAtIndex:index] atIndex:index];
}

- (void)insertObject:(id)anObject atIndex:(unsigned)anIndex
{
if (_insert)
return _insert(_proxyObject, _insertSEL, anObject, anIndex);
[self insertObjects:[anObject] atIndexes:[CPIndexSet indexSetWithIndex:anIndex]];
}

var target = [[self _representedObject] copy];
- (void)insertObjects:(CPArray)theObjects atIndexes:(CPIndexSet)theIndexes
{
if (_insertMany)
_insertMany(_proxyObject, _insertManySEL, theObjects, theIndexes);
else if (_insert)
{
var indexesArray = [];
[theIndexes getIndexes:indexesArray maxCount:-1 inIndexRange:nil];

[target insertObject:anObject atIndex:anIndex];
[self _setRepresentedObject:target];
for (var index = 0; index < [indexesArray count]; index++)
{
var objectIndex = [indexesArray objectAtIndex:index],
object = [theObjects objectAtIndex:index];

_insert(_proxyObject, _insertSEL, object, objectIndex);
}
}
else
{
var target = [[self _representedObject] copy];

[target insertObjects:theObjects atIndexes:theIndexes];
[self _setRepresentedObject:target];
}
}

- (void)removeObject:(id)anObject
Expand Down

0 comments on commit b21eab1

Please sign in to comment.