简体   繁体   English

如何更改NSMutableArray的NSNumber值?

[英]How to change the NSNumber value of the NSMutableArray?

I trying to change a NSNumber value in NSMutableArray. 我试图更改NSMutableArray中的NSNumber值。

How to do that? 怎么做?

- (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?
    } 
}

The code in your question won't change anything except the value of the loop variable. 您问题中的代码除循环变量的值外不会更改任何其他内容。

You need to replace the value in the array like this: 您需要像这样替换数组中的值:

[_numArray replaceObjectAtIndex:1 withObject:@NO];

Or if you want to replace all, then do: 或者,如果您要全部替换,请执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM