• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSArray - move Items at indexes
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

  • Follow-Ups:
    • Re: NSArray - move Items at indexes
      • From: Andreas Mayer <email@hidden>
References: 
 >NSArray - move Items at indexes (From: Alexander Cohen <email@hidden>)

  • Prev by Date: NSArray - move Items at indexes
  • Next by Date: Cocoa Books
  • Previous by thread: NSArray - move Items at indexes
  • Next by thread: Re: NSArray - move Items at indexes
  • Index(es):
    • Date
    • Thread