NSInternalInconsistencyException变异方法发送到xcode 6.1中的不可变对象

时间:2014-11-03 09:37:16

标签: objective-c nsmutablearray nsuserdefaults nsmutabledictionary

在我的办公室我们在第二个6.1中的第一个Xcode 6.0.1中有两个iMac

我们在同一个应用程序中工作,在6.0.1中一切正常,在6.1中,应用程序是Crashes。

我真的希望我能正确解释这个问题。

这是Xcode输出:

2014-11-03 10:48:57.358 appName[3282:505629] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFArray insertObject:atIndex:]: mutating method sent to immutable object'
*** First throw call stack: 

List item
...

当我点击发送按钮时,我的问题从这里开始:

-(void)sendButtonClicked
 {
NSMutableDictionary *pack = [self createMessagePack];
[Conversation addMessage:pack ToConversationWithUser:theUser];
[inputTextView setText:@""];
}

创建包:

-(NSMutableDictionary *)createMessagePack
{
NSMutableDictionary *msg = [[NSMutableDictionary alloc]init];
[msg setObject:CurrentUser.UserName forKey:@"sender"];
[msg setObject:theUser.Number forKey:@"reciever"];
[msg setObject:[NSDate dateWithTimeIntervalSinceNow:0] forKey:@"createdAt"];
[msg setObject:inputTextView.text forKey:@"text"];

return msg;
}

这里应用程序崩溃在这一行“[conversation addObject:message];”

+(void)addMessage : (NSMutableDictionary *)message ToConversationWithUser : (appUsers *)user
{
NSMutableArray *conversation = [Conversation getConversationWithUser:user];
if ([conversation count] == 0)
{
    [user savePerson];
}
[conversation addObject:message];

[Conversation saveConversation:conversation WithUser:user];
}

Conversation getConversationWithUser:用户跳转到此处

+(NSMutableArray *)getConversationWithUser : (appUsers *)user
{
NSMutableDictionary *allConversations = [Conversation getAllConversations];

NSMutableArray *theConversation = [allConversations objectForKey:user.appNumber];

if (!theConversation) // the conversation not exist yet..
{
    theConversation = [[NSMutableArray alloc]init];
}

return  theConversation;
}

[Conversation getAllConversations];跳到这里

+(NSMutableDictionary *)getAllConversations
{
NSMutableDictionary *allConversations = [[NSUserDefaults standardUserDefaults] objectForKey:@"conversations"];
if (!allConversations)
{
    allConversations = [[NSMutableDictionary alloc]init];
    [[NSUserDefaults standardUserDefaults] setObject:allConversations forKey:@"conversations"];
}
return allConversations;
}

我理解我的“NSUserDefaults standardUserDefaults”存在问题,返回不可变,但我在这里尝试的所有内容仍然无法解决。

1 个答案:

答案 0 :(得分:1)

更改:

NSMutableDictionary *allConversations = [Conversation getAllConversations];

到此:

NSMutableDictionary *allConversations = [[NSMutableDictionary alloc]initWithDictionary:[Conversation getAllConversations]];