奇怪的是“发送到不可变对象的变异方法".将对象添加到可变数组时出错 [英] Strange "mutating method sent to immutable object" error when adding an object to a mutable array

查看:73
本文介绍了奇怪的是“发送到不可变对象的变异方法".将对象添加到可变数组时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个奇怪的...

This is a strange one...

我会定期检查用户是否在我的应用程序中获得了新的最高分.这是我的代码:

Periodically I am checking if the user has achieved a new top score in my app. Here is my code:

- (void) newTopScore
{
    // pull old top score from the database
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSDictionary *getBoardsAndScores = [defaults dictionaryRepresentation];
    NSMutableArray *boardsAndScores = [[getBoardsAndScores objectForKey:@"boardsAndScores"] mutableCopy];
    NSMutableDictionary *boardAndScoreObject = [[boardsAndScores objectAtIndex:gameBoardIndex] mutableCopy];
    NSString *topscore = [boardAndScoreObject objectForKey:@"topscore"];  

    NSLog(@"topscore in value: %i", [topscore intValue]);

    if ([topscore intValue] == 0)
    {
        NSString *topscoreString = [[NSString alloc] initWithFormat:@"%i", [self countCurrentPegs]];

        NSMutableArray *mutableBoardsAndScores = [boardsAndScores mutableCopy];

        [[mutableBoardsAndScores objectAtIndex:gameBoardIndex] setObject:topscoreString forKey:@"topscore"];

        [defaults setObject:mutableBoardsAndScores forKey:@"boardsAndScores"];
        [defaults synchronize];

    }

    else if ([self countCurrentPegs] < [topscore intValue])
    {
        NSString *topscoreString = [[NSString alloc] initWithFormat:@"%i", [self countCurrentPegs]];

        NSMutableArray *mutableBoardsAndScores = [boardsAndScores mutableCopy];

        [[mutableBoardsAndScores objectAtIndex:gameBoardIndex] setObject:topscoreString forKey:@"topscore"];

        [defaults setObject:mutableBoardsAndScores forKey:@"boardsAndScores"];
        [defaults synchronize];
    }

}

有时,对于if和else来说,代码都可以正常工作.有时尽管出现以下错误:

Sometimes the code works just fine, both for the if and the else. Sometimes though I get the following error:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary setObject:forKey:]: 
mutating method sent to immutable object'

我尝试设置断点并逐行逐步执行程序.崩溃似乎有些随机,我无法制定模式.通常在第二个崩溃...

I've tried setting break points and stepping through the program line by line. The crash seems somewhat random and I can't work out a pattern. It usually crashes on the second...

[[mutableBoardsAndScores objectAtIndex:gameBoardIndex] setObject:topscoreString forKey:@"topscore"];

...行.

该对象是可变的.或者至少是.为什么会改变?

The object IS mutable. Or at least it was. Why is it changing?

任何建议的帮助将不胜感激.

Any help of advice would be very appreciated.

谢谢.

推荐答案

[[[mutableBoardsAndScores objectAtIndex:gameBoardIndex] mutableCopy] setObject:topscoreString forKey:@"topscore"];

您能测试一下吗? (为gameBoard添加了mutableCopy);

Could you test this? (added the mutableCopy for the gameBoard);

编辑

您可以将以上内容更改为:

could you change the above to:

NSMutableArray *gameBoard = [[mutableBoardsAndScores objectAtIndex:gameBoardIndex] mutableCopy];

[gameBoard setObject:topscoreString forKey:@"topscore"];

[mutableBoardsAndScores removeObjectAtIndex:gameBoardIndex];
[mutableBoardsAndScores insertObject:gameBoard atIndex:gameBoardIndex];

您可以尝试一下吗?它可能不是最漂亮的解决方案,但我相信它会起作用.

Could you try this? It might not be the most beautiful solution but i believe it will work.

这篇关于奇怪的是“发送到不可变对象的变异方法".将对象添加到可变数组时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆