From b21eab1bf15a7e5bc3149358967ff21a61bdaf92 Mon Sep 17 00:00:00 2001 From: Klaas Pieter Annema Date: Wed, 25 Aug 2010 15:50:24 +0200 Subject: [PATCH] Improved KVC mutable indexed accessors 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. --- Foundation/CPArray+KVO.j | 42 ++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/Foundation/CPArray+KVO.j b/Foundation/CPArray+KVO.j index 49ff5d621d..efdb784c8c 100644 --- a/Foundation/CPArray+KVO.j +++ b/Foundation/CPArray+KVO.j @@ -120,7 +120,7 @@ _insertManySEL = sel_getName(@"insertObjects:in"+capitalizedKey+"AtIndexes:"); if ([_proxyObject respondsToSelector:_insertManySEL]) - _insert = [_proxyObject methodForSelector:_insertManySEL]; + _insertMany = [_proxyObject methodForSelector:_insertManySEL]; _removeManySEL = sel_getName(@"removeObjectsFrom"+capitalizedKey+"AtIndexes:"); if ([_proxyObject respondsToSelector:_removeManySEL]) @@ -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 @@ -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