Object-C: NSMutableArray的removeObjectAtIndex
Object-C: NSMutableArray
测试学习书记的时候的小发现
removeObjectAtIndex会自动把后面的 object 向前补齐填充,而不会出现缺一个数字下标的情况.
例子
先把第48行的删除注释掉:
// [employees removeObjectAtIndex:1];
//
// main.m
// ObjectCTest
//
// Created by user on 15/6/8.
// Copyright (c) 2015年 Orange-W. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Employee :NSObject{
int count;
}
- (void) print: (int)count;
@end
@implementation Employee
int count=1;
- (void) print: (int)i{
NSLog(@"添加成功 %d",i);
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
}
NSMutableArray *employees = [[NSMutableArray alloc ] init];
for (int i=0; i < 5; i++) {
Employee *person = [[Employee alloc] init];
[employees addObject:person];
[[employees objectAtIndex:i] print:i];
}
NSLog(@"\n");
[employees removeObjectAtIndex:3];
for (int i=0; i < employees.count; i++) {
Employee *tmp = [employees objectAtIndex:i];
[tmp print:i];
}
// [employees removeObjectAtIndex:1];
NSLog(@"\n%lu\n ",(unsigned long)employees.count);
NSLog(@"end\n");
[[employees objectAtIndex:3] print:111];
return 0;
}
结果如下:
2015-06-08 17:37:20.214 ObjectCTest[16588:170191] 添加成功 0
2015-06-08 17:37:20.215 ObjectCTest[16588:170191] 添加成功 1
2015-06-08 17:37:20.215 ObjectCTest[16588:170191] 添加成功 2
2015-06-08 17:37:20.215 ObjectCTest[16588:170191] 添加成功 3
2015-06-08 17:37:20.215 ObjectCTest[16588:170191] 添加成功 4
2015-06-08 17:37:20.215 ObjectCTest[16588:170191]
2015-06-08 17:37:20.215 ObjectCTest[16588:170191] 添加成功 0
2015-06-08 17:37:20.215 ObjectCTest[16588:170191] 添加成功 1
2015-06-08 17:37:20.215 ObjectCTest[16588:170191] 添加成功 2
2015-06-08 17:37:20.215 ObjectCTest[16588:170191] 添加成功 3
2015-06-08 17:37:20.216 ObjectCTest[16588:170191]
4
2015-06-08 17:37:20.216 ObjectCTest[16588:170191] end
2015-06-08 17:37:20.216 ObjectCTest[16588:170191] 添加成功 111
可以发现,虽然我removeObjectAtIndex:3 的 object,可是上图并未有空缺(理论上应该是012 45),实际是01234,中间未有空缺,
说明他自动向前补齐了空缺.
继续着测试
再进行第二个测试,把第48行注释去掉,可以推断应该会报错(如果补齐了,不会存在下标为3的 object,反之不会报错)
结果不出所料:
2015-06-08 17:43:02.248 ObjectCTest[16886:174051] 添加成功 0
2015-06-08 17:43:02.249 ObjectCTest[16886:174051] 添加成功 1
2015-06-08 17:43:02.249 ObjectCTest[16886:174051] 添加成功 2
2015-06-08 17:43:02.249 ObjectCTest[16886:174051] 添加成功 3
2015-06-08 17:43:02.249 ObjectCTest[16886:174051] 添加成功 4
2015-06-08 17:43:02.249 ObjectCTest[16886:174051]
2015-06-08 17:43:02.249 ObjectCTest[16886:174051] 添加成功 0
2015-06-08 17:43:02.249 ObjectCTest[16886:174051] 添加成功 1
2015-06-08 17:43:02.249 ObjectCTest[16886:174051] 添加成功 2
2015-06-08 17:43:02.249 ObjectCTest[16886:174051] 添加成功 3
2015-06-08 17:43:02.250 ObjectCTest[16886:174051]
3
2015-06-08 17:43:02.250 ObjectCTest[16886:174051] end
2015-06-08 17:43:02.250 ObjectCTest[16886:174051] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 3 beyond bounds [0 .. 2]'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff8a9ac03c __exceptionPreprocess + 172
1 libobjc.A.dylib 0x00007fff930ec76e objc_exception_throw + 43
2 CoreFoundation 0x00007fff8a8751e4 -[__NSArrayM objectAtIndex:] + 260
3 ObjectCTest 0x0000000100000e4f main + 559
4 libdyld.dylib 0x00007fff9680f5c9 start + 1
5 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
注意看这一句:reason: '*** -[__NSArrayM objectAtIndex:]: index 3 beyond bounds [0 .. 2]'
说明他确实是自动补齐了