NSMutableArray 正在删除具有相同字符串的所有对象

发布于 2025-01-03 20:10:09 字数 417 浏览 2 评论 0原文

我正在使用一个具有相同字符串对象的 NSMutableArray。

这是代码

NSMutableArray *arr = [[NSMutableArray alloc]initWithObjects:@"hello",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",nil];
NSObject *obj = [arr objectAtIndex:2];    
[arr removeObject:obj];       
NSLog(@"%@",arr);

当我尝试删除数组的第三个对象时,它会删除带有“hi”字符串的所有对象。 我不明白为什么会发生。
我的疑问是在删除对象时, NSMutableArray 匹配字符串或地址。

I am using one NSMutableArray with same string object.

Here is the code

NSMutableArray *arr = [[NSMutableArray alloc]initWithObjects:@"hello",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",nil];
NSObject *obj = [arr objectAtIndex:2];    
[arr removeObject:obj];       
NSLog(@"%@",arr);

When i try to remove 3rd object of array, its removing all object with "hi" string.
I am not getting why its happening.
my doubt is while removing object, NSMutableArray match string or address.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

街角卖回忆 2025-01-10 20:10:09

这是因为您使用的是 removeObject ,它会删除与您传入的对象“相等”的所有对象。根据 此 Apple 文档

此方法使用indexOfObject: 来定位匹配项,然后删除
通过使用removeObjectAtIndex: 来删除它们。因此,比赛是根据
对象对 isEqual: 消息的响应的基础。如果
array 不包含 anObject,该方法没有效果(尽管它
确实会产生搜索内容的开销)。

您将看到效果文字字符串,其中每个 @"hi" 对象将成为刚刚添加多次的同一个对象。

你真正想做的是:

NSMutableArray *arr = [[NSMutableArray alloc]initWithObjects:@"hello",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",nil];
[arr removeObjectAtIndex:2];
NSLog(@"%@",arr);

然后你专门删除索引 2 处的对象。

It's because you're using removeObject which removes all objects that are "equal" to the one you pass in. As per this Apple documentation:

This method uses indexOfObject: to locate matches and then removes
them by using removeObjectAtIndex:. Thus, matches are determined on
the basis of an object’s response to the isEqual: message. If the
array does not contain anObject, the method has no effect (although it
does incur the overhead of searching the contents).

You're seeing the effects of literal strings here where each of those @"hi" objects will turn out to be the same object just added many times.

What you really want to do is this:

NSMutableArray *arr = [[NSMutableArray alloc]initWithObjects:@"hello",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",nil];
[arr removeObjectAtIndex:2];
NSLog(@"%@",arr);

Then you're specifically removing the object at index 2.

独闯女儿国 2025-01-10 20:10:09

根据 https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html

删除对象:

删除给定对象数组中所有出现的位置。

这正是您所看到的行为。如果要删除特定位置的对象,则需要removeObjectAtIndex:

According to https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html

removeObject:

Removes all occurrences in the array of a given object.

which is exactly the behaviour you're seeing. If you want to remove the object at a particular position, you want removeObjectAtIndex:.

不寐倦长更 2025-01-10 20:10:09
NSMutableArray *arr = [[NSMutableArray alloc]initWithObjects:@"hello",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",nil];
NSUInteger obj = [arr indexOfObject:@"hi"];  //Returns the lowest integer of the specified object
[arr removeObjectAtIndex:obj];  //removes the object from the array
NSLog(@"%@",arr);
NSMutableArray *arr = [[NSMutableArray alloc]initWithObjects:@"hello",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",nil];
NSUInteger obj = [arr indexOfObject:@"hi"];  //Returns the lowest integer of the specified object
[arr removeObjectAtIndex:obj];  //removes the object from the array
NSLog(@"%@",arr);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文