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: Thu, 20 Sep 2007 08:27:10 -0700
On 9/20/07, Alexander Cohen <email@hidden> wrote:
> Is there any code out there to do something like a [NSArray
> moveObjectsAtIndexes:(NSindexSet*)indexes toIndex:(unsigned)index]; Im
> really having a hard time wrapping my head around how to do that.
First, you need an NSMutableArray (as NSArray instances are immutable).
Second, you can remove objects at the old indexes, and re-insert them
at the new index. Something along the lines of this (warning composed
in mail, not tested, but should give you the basic idea):
@implementation NSMutableArray(CSCAdditions)
-(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];
NSEnumerator *enumerator = [objectsToMove objectEnumerator];
id object = nil;
while(object = [enumerator nextObject]) {
[self insertObject: object atIndex: index++];
}
}
@end
--
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