objective-c 如何删除Objective C中数组的第一个元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1009709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I remove the first element of an array in Objective C?
提问by ojreadmore
In Objective C, is there a one-liner or something small to remove (shorten by one) and return the first element of an array, regardless of its index?
在目标 C 中,是否有一个单行或一些小的东西要删除(缩短一)并返回数组的第一个元素,而不管其索引如何?
回答by Jeffrey Hantin
I don't know of a method that returns the item removed, but you can do this using a combination of NSArray#objectAtIndex:0and NSMutableArray#removeObjectAtIndex:0. I suppose you could introduce a new method category on NSMutableArraythat implements a shiftmethod.
我不知道返回项中移除的方法,但你可以使用这个的组合做NSArray#objectAtIndex:0和NSMutableArray#removeObjectAtIndex:0。我想你可以引入一个新的方法类别NSMutableArray来实现一个shift方法。
回答by Bill K
That would be a poor thing to do.
那将是一件糟糕的事情。
Objective-C on the iPhone can actually use most of the performance perks of C.
iPhone 上的 Objective-C 实际上可以使用 C 的大部分性能优势。
If you look at some of my other posts, you'll see I'm ADAMANTLY against premature optimization, but when you are coding at the C level, there are just some things you don't do unnecessarilly.
如果您查看我的其他一些帖子,您会发现我坚决反对过早优化,但是当您在 C 级别进行编码时,您不会不必要地做一些事情。
- 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)
- 移动内存
- 重复结构
- 分配稀疏填充的内存块
- 内循环
- ...(还有很多,但我的 C-life 生锈了,正如我所说,我反对优化)
What you probably want is a well-implemented queue. Something that pre-allocates a large enough circular memory structure and then has two pointers that track the first and last bytes.
您可能想要的是一个实施良好的队列。预先分配足够大的循环内存结构,然后有两个指针来跟踪第一个和最后一个字节的东西。
I'd be pretty surprised to hear that Objective-C didn't have a queue data structure.
听到 Objective-C 没有队列数据结构,我会非常惊讶。
Also, don't strive for the one-liners. All the stuff about terse code is overrated. If it makes more sense to call a method, so be it.
另外,不要为单线而努力。所有关于简洁代码的东西都被高估了。如果调用方法更有意义,那就这样吧。
回答by StilesCrisis
It's certainly too late to assist the original poster, but if you have a plain NSArray and not an NSMutableArray, this works well:
现在帮助原始海报肯定为时已晚,但如果您有一个普通的 NSArray 而不是 NSMutableArray,这很有效:
id myData = myArray.firstObject;
myArray = [myArray subarrayWithRange:NSMakeRange(1, myArray.count - 1)];
回答by Blindy
If you have an array obj *arrwhere objis a class/typename and arris the array, you can just say arr+1to get the array without the first element.
如果你有一个数组obj *arr,其中obj是一个类/类型名并且arr是数组,你可以说arr+1获取没有第一个元素的数组。
回答by Quinn Taylor
Cocoa array objects (NSArray/NSMutableArray) do not provide a one-line equivalent — you would have to read the object first, then remove it. The fact that these classes provide the methods -lastObjectand -removeLastObjectbut not -firstObjectand -removeFirstObjectshould be a reminder that removing from the front of an array is usually an inefficient operation, since the contents must be shifted (copied) one position forward. This is particular true for arrays in C, which are intrinsically tied with pointers.
Cocoa 数组对象 (NSArray/NSMutableArray) 不提供单行等效项 — 您必须先读取对象,然后将其删除。事实上,这些类提供了方法-lastObject,-removeLastObject但不提供-firstObject并且-removeFirstObject应该提醒我们从数组的前面删除通常是一种低效的操作,因为内容必须向前移动(复制)一个位置。这对于 C 中的数组尤其如此,它们本质上与指针联系在一起。
If you're working with anything but primitive data types and/or very small arrays, you might want to consider that the behavior of "shifting off" the first element is indicative of a queue data structure. For details on how you might create a queue for objects, see this SO question. Personally, my opinion for that questionis that a real queue class provides the cleanest programming idiom. You can even define your own method (perhaps as a category on NSMutableArray or another class) that doesprovide a one-liner to do what you want:
如果您使用的不是原始数据类型和/或非常小的数组,您可能需要考虑“移出”第一个元素的行为表示队列数据结构。有关如何为对象创建队列的详细信息,请参阅此 SO 问题。就我个人而言,我对这个问题的看法是真正的队列类提供了最干净的编程习惯。您甚至可以定义自己的方法(可能作为 NSMutableArray 或其他类上的一个类别),它确实提供了一个单行代码来执行您想要的操作:
@interface NSMutableArray (QueueOneLiner)
- (id) removeAndReturnFirstObject; // Verbose, but clearer than "shift"
@end
@implementation NSMutableArray (QueueOneLiner)
- (id) removeAndReturnFirstObject {
id object = [[self objectAtIndex:0] retain];
[self removeObjectAtIndex:0];
return [object autorelease];
}
@end
However, by that point the solution will likely cause more overhead than it's worth, depending on the importance you place on simplicity versus performance of the code that uses it.
但是,到那时,解决方案可能会导致比其价值更多的开销,这取决于您对使用它的代码的简单性与性能的重视程度。
回答by Nithinbemitk
Use this code,
使用此代码,
[arrayName removeObjectAtIndex:0];
this may help you
这可能会帮助你

