popToRootViewControllerAnimated时随机数据丢失(NSUserDefaults)

时间:2013-10-03 18:01:59

标签: ios objective-c nsuserdefaults

我的应用有时会在使用时丢失所有数据:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     */

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    switch (indexPath.row)
    {
        case kLANG_HEBREW:
            // Hebrew is the default
            self.arrayOfLanguages = [NSMutableArray arrayWithObjects:@"he", @"en", nil];
            break;
        case kLANG_ENGLISH:
            self.arrayOfLanShorts = [NSMutableArray arrayWithObjects:@"en", @"he", nil];
            break;
        case kLANG_RUSSIAN:
            self.arrayOfLanShorts = [NSMutableArray arrayWithObjects:@"ru", @"en", nil];
            break;
        case kLANG_ARABIC:
            self.arrayOfLanShorts = [NSMutableArray arrayWithObjects:@"ar", @"en", nil];
            break;
    }
    [[NSUserDefaults standardUserDefaults] setObject:self.arrayOfLanShorts forKey:@"AppleLanguages"];
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"come_from_change_lang"];
    [[NSUserDefaults standardUserDefaults] synchronize];    
    [self.navigationController popToRootViewControllerAnimated:YES];
}

这很奇怪,因为我没有清除我的应用程序中的数据。

1 个答案:

答案 0 :(得分:0)

我的猜测是你的switch语句并不总是设置self.arrayOfLanShorts,然后是nil。然后,当您在我们的用户默认值中设置它时,它会删除存储的值,看起来您丢失了数据。在将数组添加到默认值之前,我会检查一个if语句,表明你的数组不是nil,如果它是跟踪它没有命中你的switch语句的话。