小萌在做开发拖拽单元格移动的时候遇到的坑,这是一个小萌忽略的问题。removeObjectAtIndex和removeObject的两者虽然都是删除,可是却有很大的不同
1、removeObject删除对象
api上是这么解释的。
Removes all occurrences in the array of a given object.
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).
删除NSMutableArray中所有isEqual:待删对象的对象
也就是说removeObject:可以删除多个对象(只要符合isEqual:的都删除掉,只要两个对象相等都可以删除掉)。
2、removeObjectAtIndex
api上这样解释的。
Removes the object at index .
To fill the gap, all elements beyond index are moved by subtracting 1 from their index.
Important:Important
Raises an exception NSRangeException if index is beyond the end of the array.
删除指定NSMutableArray中指定index的对象,注意index不能越界,只能删除一个元素。
3、举例说明
tempArray 临时数组
[@"how", @"to", @"remove", @"remove", @"object",@"remove", @"true"]
就拿上面的数组举例吧
(一)removeObject删除
[tempArray removeObject:@"remove"];
输出结果:
//所有@"remove"都被删掉
[@"how", @"to", @"object", @"true"]
(二)removeObjectAtIndex删除
[tempArray removeObjectAtIndex:2];
输出结果:
只删掉一个下标为2的@"remove"元素,其他的都还在
[@"how", @"to", @"remove", @"object",@"remove", @"true"]