简体   繁体   English

在范围内的NSMutableArray中删除对象

[英]remove object at NSMutableArray in range

I am trying to remove objects at array starting at index 5 to the end of the list. 我试图删除从索引5开始到列表末尾的数组中的对象。 I have a for loop to do this, however now I discovered 我有一个for循环来做这个,但现在我发现了

 - (void)removeObject:(id)anObject inRange:(NSRange)aRange

The question is what is the anObject here? 问题是这里的anObject是什么? I only need range as far as I know 据我所知,我只需要射程

removeObject:inRange: deletes an object within a certain range. removeObject:inRange:删除特定范围内的对象。 This method would be useful if you wanted delete the string @"Hello World" only if it is one of the first 5 elements. 如果您希望删除字符串@"Hello World"只有它是前5个元素之一,此方法才有用。

It sounds like what you are trying to do is delete all objects after the 5th element. 听起来你要做的就是删除第5个元素之后的所有对象。 If that is what you are trying to do, you should use the removeObjectsInRange: method. 如果这是您要执行的操作,则应使用removeObjectsInRange:方法。 For example: 例如:

 NSRange r;
 r.location = 5;
 r.length = [someArray count]-5;

 [someArray removeObjectsInRange:r];

You want 你要

- (void)removeObjectsInRange:(NSRange)aRange

Removes from the array each of the objects within a given range. 从数组中删除给定范围内的每个对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM