简体   繁体   English

NSUserDefaults在iPhone模拟器中始终崩溃

[英]NSUserDefaults crashes consistently in iPhone Simulator

Before I begin I'll freely admit it's something in my code but it's not blatantly obvious what's causing it. 在我开始之前,我将自由地承认这是我的代码中的某些内容,但是并不清楚原因是什么。

I'm storing one boolean and four NSDate objects in NSUserDefaults. 我在NSUserDefaults中存储一个布尔值和四个NSDate对象。 I have a view that loads the information into controls: boolean into a switch and the 4 date values are loaded as the label on 4 buttons as formatted values. 我有一个视图,它将信息加载到控件中:布尔值加载到开关中,并且4个日期值作为4个按钮上的标签加载为格式化值。 If the user clicks on a button I switch to a new view with a UIDatePicker that is preloaded with the full date from NSUserDefaults based on the button that was clicked. 如果用户单击按钮,我将切换到带有UIDatePicker的新视图,该视图已基于单击的按钮从NSUserDefaults中预加载了完整日期。 The 2nd view has two buttons: cancel and set. 第二个视图有两个按钮:取消和设置。 If you choose cancel we switch back to the first view. 如果选择取消,我们将切换回第一个视图。 If you choose set the value from the UIDatePicker is stored in NSUserDefaults, the chosen button's label is updated, and we switch back to the first view. 如果选择设置,则将UIDatePicker中的值存储在NSUserDefaults中,则更新所选按钮的标签,然后切换回第一个视图。

I can consistently crash the application by clicking a button and then click cancel three times in a row, and then clicking the button a fourth time. 我可以通过单击一个按钮,然后连续单击取消三次,然后单击第四次单击来使应用程序持续崩溃。 It crashes in objc_msgSend when retrieving the value out of NSUserDefaults. 从NSUserDefaults中检索值时,它在objc_msgSend中崩溃。

Here's the 3 functions involved. 这是其中涉及的三个功能。

  - (IBAction) buttonPressed:(id)sender {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSDate *buttonDate;

    switch ([sender tag]) {
        case 1000:
            pressedString = kStartTime1Key;
            pressedButton = start1;
            break;
        case 1001:
            pressedString = kEndTime1Key;
            pressedButton = end1;
            break;
        case 1002:
            pressedString = kStartTime2Key;
            pressedButton = start2;
            break;
        case 1003:
            pressedString = kEndTime2Key;
            pressedButton = end2;
            break;
        default:
            break;
    }

    buttonDate = [defaults objectForKey:pressedString];
    if (buttonDate == nil) buttonDate = [NSDate date];
    [datePicker setDate:buttonDate animated:NO];
    [buttonDate release];

    self.view = datePickerView;
}

- (IBAction) savePressed {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSDate *selected = [datePicker date];

    [defaults setObject:selected forKey:pressedString];
    [pressedButton setTitle:[self parseDate:selected] forState:UIControlStateNormal];

    self.view = settingsView;
}

- (IBAction) cancelPressed {
    self.view = settingsView;
}

Being an iPhone and Obj-C newbie, I'm sure there are better ways to do things, I'm open to suggestions. 作为iPhone和Obj-C的新手,我敢肯定还有更好的做事方法,我愿意接受建议。

[buttonDate release]; [buttonDate发布];

You can't release something you didn't allocate or retain. 您无法释放未分配或保留的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM