简体   繁体   中英

How to change the NSNumber value of the NSMutableArray?

I trying to change a NSNumber value in NSMutableArray.

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];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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