簡體   English   中英

如何更改NSMutableArray的NSNumber值?

[英]How to change the NSNumber value of the 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?
    } 
}

您問題中的代碼除循環變量的值外不會更改任何其他內容。

您需要像這樣替換數組中的值:

[_numArray replaceObjectAtIndex:1 withObject:@NO];

或者,如果您要全部替換,請執行以下操作:

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