#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
@autoreleasepool {
NSMutableArray *changeme =[NSMutableArray arrayWithCapacity:25];
//[changeme addObject:[NSNumber numberWithInt:2]];
for (int i=2; i<=100;i+=2)
{
[changeme addObject:[NSNumber numberWithInt:i]];
}
for(int x=0;x<[changeme count];x++)
{
NSLog(@"Item here is %i",[[changeme objectAtIndex:x]intValue]);
}
[changeme removeObjectAtIndex:2];
return 0;
}
}
removeObjectAtIndex 删除索引处的对象。我的程序应该打印 2-100 中的所有偶数。所以,index[2]=6
应该被删除。但是,它没有做出应有的 react 。
最佳答案
此处仅在打印项目后才删除对象。
如果移动代码
[changeme removeObjectAtIndex:2];
开始第二个 for
循环,然后你会看到 index[2] = 6,被删除了。
所以应该是这样的:
for (int i=2; i<=100;i+=2)
{
[changeme addObject:[NSNumber numberWithInt:i]];
}
[changeme removeObjectAtIndex:2];
for(int x=0;x<[changeme count];x++)
{
NSLog(@"Item here is %i",[[changeme objectAtIndex:x]intValue]);
}
关于iphone - NSMutableArray 和 removeobjectsatindex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8664120/