iphone - NSMutableArray 和 removeobjectsatindex

标签 iphone objective-c ios ios5

#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/

相关文章:

ios - RuTube MPMoviePlayerController initWithContentURL :

iphone - ui按钮动画

iphone - 有没有办法从 ios 中的 mp3 中提取嵌入的图像数据

ios - Monotouch-启用Guard Malloc

ios - 显示键盘时在 iPad 上的表单中调整表格 View 的大小

ios - 协议(protocol)委托(delegate)方法和 block 之间的连接区别

ios - 我可以在低电量时唤醒 iOS 应用程序吗?如何?

iphone - Appstore-仅包含YouTube视频的应用程序会被接受吗?

ios - iphone nslog 损坏的数据

iphone - 子类 UIAlertView 在 iOS 4.2 中未正确绘制