• 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: 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

References: 
 >NSArray - move Items at indexes (From: Alexander Cohen <email@hidden>)
 >Re: NSArray - move Items at indexes (From: "Clark Cox" <email@hidden>)
 >Re: NSArray - move Items at indexes (From: Andreas Mayer <email@hidden>)

  • Prev by Date: Re: NSArray - move Items at indexes
  • Next by Date: Re: Question about scope of "convenience objects"
  • Previous by thread: Re: NSArray - move Items at indexes
  • Next by thread: Re: NSArray - move Items at indexes
  • Index(es):
    • Date
    • Thread