Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

update value at specific index in NSArray

I am hoping not to need to use an NSMutableArray here. I have an array with 10 elements. I want to change the value at index 4. Is there a way to do this without having to use NSMutableArray? The problem with NSMutableArray is that we can change anything about it, including its size. I don't want the size of this array to change accidentally. I just want to change the value at index 4 from say 22 to 25. How might I do that? doing array[4]=25 is not working.

like image 450
Katedral Pillon Avatar asked Nov 21 '25 06:11

Katedral Pillon


1 Answers

NSArray *ar1 = @[@"1",@"2"];
NSMutableArray *ar1update = [ar1 mutableCopy];
ar1update[1] = @"Changed";
ar1 = [NSArray arrayWithArray:ar1update];
like image 169
Dreaming In Binary Avatar answered Nov 22 '25 21:11

Dreaming In Binary