应用在NSUserDefaults setObject:forKey上崩溃:

时间:2018-10-11 07:41:32

标签: objective-c nsuserdefaults nsnotificationcenter ios12

将对象设置为NSUserDefaults时,我的应用在iOS 12中随机崩溃。以下是崩溃发生的代码

shared2 = [[NSUserDefaults alloc]initWithSuiteName:@"group.AppName"];

NSData *datashare = [NSKeyedArchiver archivedDataWithRootObject:_sharedData];

[shared2 setObject:datashare forKey:@"dicForToday"];

没有获得任何崩溃日志,因为它是随机发生的

此崩溃的频率在iOS 12上更多。

2 个答案:

答案 0 :(得分:1)

通常,您可以使用此方法保存数据 发布方式:

 [[NSUserDefaults standardUserDefaults] setObject:object forKey:@"somename"];

获取方法:

NSMutableDictionary *dicCover = [[NSUserDefaults standardUserDefaults] valueForKey:@"somename"];

有时,字典或数组是否正确格式化,或者我们无法使用正确的格式来获取您的数据,例如使用NSUserdefault 使用Achive:

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:arrayUsers];
    [[NSUserDefaults standardUserDefaults] setObject:data forKey:@"somename"];

获得:

NSData *usersData = [[NSUserDefaults standardUserDefaults] valueForKey:@"somename"];
    NSMutableArray *redirectionDic = [NSKeyedUnarchiver unarchiveObjectWithData:usersData];

答案 1 :(得分:-2)

  NSUserDefaults *myDefaults = [[NSUserDefaults alloc]
                                  initWithSuiteName:keyChainGroup];
    [myDefaults setObject:userInfo forKey:key];

尝试一下