[英]Changing integer values in NSMutableArray, IOS Objective-C
I created an array that would hold some integers.我创建了一个包含一些整数的数组。 However I seem to get an error on a line of code which is trying to alter one of the values in the array.
但是,我似乎在尝试更改数组中的一个值的代码行中遇到错误。 I have created an array as follows:
我创建了一个数组,如下所示:
NSMutableArray *lockedArray;
lockedArray = [NSMutableArray arrayWithCapacity:50];
I have added some values into the array:我在数组中添加了一些值:
[lockedArray addObject:@10];
[lockedArray addObject:@20];
[lockedArray addObject:@30];
[lockedArray addObject:@40];
[lockedArray addObject:@50];
[lockedArray addObject:@60];
[lockedArray addObject:@70];
[lockedArray addObject:@80];
Now I want to change one of the values.现在我想更改其中一个值。 For example if I want to change the 6th value then I use the following code...
例如,如果我想更改第 6 个值,则使用以下代码...
[lockedArray replaceObjectAtIndex:5 withObject:@1];
In Xcode I do not get any warning and it all looks fine.在 Xcode 中,我没有收到任何警告,一切看起来都很好。 When I run the code and initiate replacing an object it crashes and gives me the following error...
当我运行代码并开始替换一个对象时,它会崩溃并给我以下错误...
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFArray replaceObjectAtIndex:withObject:]: mutating method sent to immutable object'
由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“-[__NSCFArray replaceObjectAtIndex:withObject:]: mutating method sent to immutable object”
We don't know what you are doing exactly, but at the point where replaceObjectAtIndex is called, lockedArray is not a mutable array but an immutable array.我们不知道你到底在做什么,但在调用 replaceObjectAtIndex 时,lockedArray 不是可变数组而是不可变数组。
Since an array cannot be changed from being mutable to immutable, you most likely assigned a different (immutable) array to lockedArray after you added the other elements.由于数组不能从可变变为不可变,因此您很可能在添加其他元素后将不同的(不可变的)数组分配给了 lockedArray。
Have u allocated and initialized ur array like this,你有没有像这样分配和初始化你的数组,
lockedArray = [[NSMutableArray alloc]init];
Try it, Hope it will work for u试试看,希望它对你有用
Make sure you check that you haven't pointed lockedArray to a non mutable array or you haven't initiated it as such.确保你检查你没有将lockedArray 指向一个非可变数组,或者你没有像这样启动它。 Use Xcode's find function to search for
使用 Xcode 的 find 功能进行搜索
"lockedArray = [NSArray"
“lockedArray = [NSArray”
or或者
"lockedArray ="
“锁定阵列=”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.