0

所以我正在构建一个笔记应用程序,在单击按钮后将笔记保存到 NSUSerDefaults。好吧,当我第一次加载该视图并添加注释并单击按钮进行保存时,它可以正常工作。但是,如果我不离开视图并添加新消息并尝试单击按钮进行保存,我的应用程序就会崩溃。我真的不明白为什么。我现在使用的 xcode 版本没有给我很多关于我的错误的细节,但我确实看到了

0x00015a9b  <+1175>  xor    %eax,%eax
Program received signal 'SIGABRT'

这是按钮单击(发送事件)后触发的 IBAction 的代码

-(IBAction)saveNote{
NSUserDefaults *prefs=[NSUserDefaults standardUserDefaults];
//If empty add text to synthesized array and then add the array to the USer defaults
if([[prefs stringArrayForKey:@"Subjects"] count]==0){
    NSLog(@"Went through here");
    [notesSubject addObject:writeNote.text];
    [prefs setObject:notesSubject forKey:@"Subjects"];

    NSLog(@"The length of the array is %d",[notesSubject count]);

    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Note Saved" message:@"Your note was saved" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    //[notesView.tableView reloadData];

}
//If not empty, get the array stored in the user defaults and set it to a temp array, add text to that temp array and then place the temp array back to the user defaults.
else{
    newSubjects=[[NSMutableArray alloc]init];
    beforeSubjects=[[NSArray alloc]init];

    newSubjects= [prefs stringArrayForKey:@"Subjects"];
    [newSubjects addObject:writeNote.text];
    [prefs setObject:newSubjects forKey:@"Subjects"];
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Note Saved" message:@"Your note was saved" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    //[notesView.tableView reloadData];
}


}

是的,我真的不明白为什么应用程序在第二次点击时崩溃。

4

1 回答 1

0

您正在newSubjects使用以下行用不可变实例覆盖可变实例:

newSubjects= [prefs stringArrayForKey:@"Subjects"];

相反,它应该是这样的:

newSubjects = [[prefs stringArrayForKey:@"Subjects"] mutableCopy];

于 2013-09-17T00:12:50.350 回答