Re: NSArray - move Items at indexes
Re: NSArray - move Items at indexes
- Subject: Re: NSArray - move Items at indexes
- From: "Clark Cox" <email@hidden>
- Date: Fri, 21 Sep 2007 10:47:00 -0700
On 9/20/07, Andreas Mayer <email@hidden> wrote:
>
> Am 20.09.2007 um 17:27 Uhr schrieb Clark Cox:
>
> > NSEnumerator *enumerator = [objectsToMove objectEnumerator];
> > id object = nil;
> >
> > while(object = [enumerator nextObject]) {
> > [self insertObject: object atIndex: index++];
> > }
>
> Instead of this loop you could use -
> replaceObjectsInRange:withObjectsFromArray:.
Indeed, good catch:
-(void) moveObjectsAtIndexes:(NSindexSet*)indexes toIndex:(unsigned)index
{
NSArray *objectsToMove = [self objectsAtIndexes: indexes];
//If any of the removed objects come before the index, we want to
decrement the index appropriately
index -= [indexes countOfIndexesInRange: (NSRange){0, index}];
[self removeObjectsAtIndexes: indexes];
[self replaceObjectsInRange: (NSRange){index,0}
withObjectsFromArray: objectsToMove];
}
... much cleaner.
--
Clark S. Cox III
email@hidden
_______________________________________________
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