简体   繁体   English

更改 NSMutableArray 中的整数值,IOS Objective-C

[英]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.

相关问题 iOS / Objective-C:将NSString对象添加到NSMutableArray,NSMutableArray为(空) - iOS/Objective-C: Adding NSString object to NSMutableArray, NSMutableArray is (null) 如何将2维的整数NSArrays对象添加到Objective-C中的NSMutableArray? - How to add integer NSArrays objects with 2 dimention to NSMutableArray in Objective-C? Objective-C IOS Xcode:无法修改NSMutableArray中的对象 - Objective-C IOS Xcode: Unable to modify object in NSMutableArray objective-C 中的 NSMutableArray 插入 - NSMutableArray insertion in objective-C 如何将NSMutableArray添加到NSMutableArray Objective-c - How to add an NSMutableArray to an NSMutableArray Objective-c Objective-C:NSMutableArray已初始化并正确添加,但是无法检索值 - Objective-C: NSMutableArray initialized and added to properly, but values cannot be retrieved 为什么在Objective-C和iOS中,[[NSMutableArray alloc] init]发出警告,但NSMutableArray.new却没有? - Why in Objective-C and iOS, [[NSMutableArray alloc] init] gave warning but NSMutableArray.new didn't? iOS / Objective-C:排除空值 - IOS/Objective-C: Exclude empty values 在 iOS 和 Objective-C 的 NSMutableDictionary 中添加值 - Add values in NSMutableDictionary in iOS with Objective-C Objective-C:按工作日对NSDate的NSMutableArray进行排序 - Objective-C: Sort NSMutableArray of NSDates by weekday
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM