[ACCEPTED]-How can I remove the first element of an array in Objective C?-arrays

Accepted answer
Score: 24

I don't know of a method that returns the 4 item removed, but you can do this using 3 a combination of NSArray#objectAtIndex:0 and NSMutableArray#removeObjectAtIndex:0. I suppose you could 2 introduce a new method category on NSMutableArray that 1 implements a shift method.

Score: 4

That would be a poor thing to do.

Objective-C 16 on the iPhone can actually use most of the 15 performance perks of C.

If you look at some 14 of my other posts, you'll see I'm ADAMANTLY 13 against premature optimization, but when 12 you are coding at the C level, there are 11 just some things you don't do unnecessarilly.

  • Move memory
  • Duplicate structures
  • Allocate sparsely populated memory blocks
  • Inner loops
  • ... (There are lots more, but my C-life is rusty and, as I said, I'm anti-optimization)

What 10 you probably want is a well-implemented 9 queue. Something that pre-allocates a large 8 enough circular memory structure and then 7 has two pointers that track the first and 6 last bytes.

I'd be pretty surprised to hear 5 that Objective-C didn't have a queue data 4 structure.

Also, don't strive for the one-liners. All 3 the stuff about terse code is overrated. If 2 it makes more sense to call a method, so 1 be it.

Score: 2

It's certainly too late to assist the original 2 poster, but if you have a plain NSArray 1 and not an NSMutableArray, this works well:

id myData = myArray.firstObject;
myArray = [myArray subarrayWithRange:NSMakeRange(1, myArray.count - 1)];
Score: 1

Cocoa array objects (NSArray/NSMutableArray) do 25 not provide a one-line equivalent — you 24 would have to read the object first, then 23 remove it. The fact that these classes provide 22 the methods -lastObject and -removeLastObject but not -firstObject and -removeFirstObject should be 21 a reminder that removing from the front 20 of an array is usually an inefficient operation, since 19 the contents must be shifted (copied) one 18 position forward. This is particular true 17 for arrays in C, which are intrinsically 16 tied with pointers.

If you're working with 15 anything but primitive data types and/or 14 very small arrays, you might want to consider 13 that the behavior of "shifting off" the 12 first element is indicative of a queue data structure. For details 11 on how you might create a queue for objects, see 10 this SO question. Personally, my opinion for that question is that a real queue class 9 provides the cleanest programming idiom. You 8 can even define your own method (perhaps 7 as a category on NSMutableArray or another 6 class) that does provide a one-liner to do what 5 you want:

@interface NSMutableArray (QueueOneLiner)
  - (id) removeAndReturnFirstObject; // Verbose, but clearer than "shift"

@implementation NSMutableArray (QueueOneLiner)
  - (id) removeAndReturnFirstObject {
    id object = [[self objectAtIndex:0] retain];
    [self removeObjectAtIndex:0];
    return [object autorelease];

However, by that point the solution 4 will likely cause more overhead than it's 3 worth, depending on the importance you place 2 on simplicity versus performance of the 1 code that uses it.

Score: 1

If you have an array obj *arr where obj is a class/typename 2 and arr is the array, you can just say arr+1 to 1 get the array without the first element.

Score: 0

Use this code,

 [arrayName removeObjectAtIndex:0];

this may help you


More Related questions