如何更改NSMutableArray的NSNumber值?

时间:2014-11-11 00:42:50

标签: ios objective-c nsmutablearray

我尝试在NSMutableArray中更改NSNumber值。

怎么做?

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    NSMutableArray *_numArray = [[NSMutableArray alloc] init];
    [_numArray addObject:[NSNumber numberWithBool:YES]];
    [_numArray addObject:[NSNumber numberWithBool:YES]];
    [_numArray addObject:[NSNumber numberWithBool:YES]];


    for (NSNumber *_numObject in _numArray) {
        // How to change all NSNumber Object from YES to NO?
        _numObject = [NSNumber numberWithBool:NO]; // <== Is this correct?
    } 
}

1 个答案:

答案 0 :(得分:4)

除了循环变量的值之外,你问题中的代码不会改变任何东西。

您需要像这样替换数组中的值:

[_numArray replaceObjectAtIndex:1 withObject:@NO];

或者,如果您想要全部替换,请执行:

for (NSInteger i = 0; i < _numArray.count; i++) {
    [_numArray replaceObjectAtIndex:i withObject:@NO];
}