我正在使用一個具有相同字符串對象的NSMutableArray。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匹配字符串或地址。
錯字警告:'removeObjectAtInded'應該在末尾有一個'x' :-)。 – 2012-02-08 14:21:57
有關字符串文字的其他信息,請參見:http://stackoverflow.com/a/25798/250164 – 2012-02-08 14:29:45
更正:'removeObject'方法不會刪除所有相同的對象。相反,它只消除它的一個事件。爲了移除所有「相等」的對象,我們必須使用'removeObjectIdendicalTo'方法。 – santobedi 2017-08-04 07:26:32