gpt4 book ai didi

ios - NSUserDefaults setObject 崩溃

转载 作者:行者123 更新时间:2023-11-29 00:07:36 27 4
gpt4 key购买 nike

我有一个单例类,在它的 init 方法中,我设置了一个 NSMutableArray,稍后当我保存它时,应用程序随机崩溃并显示这个崩溃日志,我很确定 recordUploadCnt 不能为nil,但是找不到哪里出错了。

  NSString* const KEY_SENSOR_UPLOAD = @"recordUploadCnt";
@property(nonatomic, strong) NSMutableArray* recordUploadCnt;

- (id)init {
if (self = [super init]) {
NSMutableArray* cnt = [[NSUserDefaults standardUserDefaults] objectForKey:KEY_SENSOR_UPLOAD];
self.recordUploadCnt = cnt ? [NSMutableArray arrayWithArray:cnt] : [NSMutableArray new];
}
return self;
}

double currentTime = [[NSDate date] timeIntervalSince1970];
if ([self.recordUploadCnt count] < 6) {
[self.recordUploadCnt addObject:[NSString stringWithFormat:@"%f",currentTime]];
} else {
[self.recordUploadCnt removeObjectAtIndex:0];
[self.recordUploadCnt addObject:[NSString stringWithFormat:@"%f",currentTime]];
}
[[NSUserDefaults standardUserDefaults] setObject:self.recordUploadCnt forKey:KEY_SENSOR_UPLOAD];
[[NSUserDefaults standardUserDefaults] synchronize];

Exception Type: SIGSEGV
Exception Codes: SEGV_MAPERR at 0xebfecbeb8
Crashed Thread: 37
Thread 37 Crashed:
0 libobjc.A.dylib 0x0000000184980430 _objc_msgSend :16 (in libobjc.A.dylib)
1 CoreFoundation 0x00000001856bf4c8 __CFPropertyListIsValidAux :52 (in CoreFoundation)
2 CoreFoundation 0x00000001856c02a4 __CFPropertyListIsArrayPlistAux :40 (in CoreFoundation)
3 CoreFoundation 0x00000001855e3900 _CFArrayApplyFunction :80 (in CoreFoundation)
4 CoreFoundation 0x00000001856bf5fc __CFPropertyListIsValidAux :360 (in CoreFoundation)
5 CoreFoundation 0x00000001855fc414 _CFPropertyListWrite :96 (in CoreFoundation)
6 CoreFoundation 0x00000001855fbc14 _CFPropertyListCreateData :316 (in CoreFoundation)
7 CoreFoundation 0x0000000185778f28 _CFPrefsEncodeKeyValuePairIntoMessage :504 (in CoreFoundation)
8 CoreFoundation 0x0000000185689f94 -[CFPrefsPlistSource sendMessageSettingValue:forKey:] :136 (in CoreFoundation)
9 CoreFoundation 0x00000001856886ec -[CFPrefsPlistSource alreadylocked_setValues:forKeys:count:from:] :440 (in CoreFoundation)
10 CoreFoundation 0x0000000185756034 -[CFPrefsSource setValues:forKeys:count:removeValuesForKeys:count:from:] :220 (in CoreFoundation)
11 CoreFoundation 0x00000001856b9150 -[CFPrefsSearchListSource alreadylocked_setValues:forKeys:count:from:] :584 (in CoreFoundation)
12 CoreFoundation 0x0000000185756034 -[CFPrefsSource setValues:forKeys:count:removeValuesForKeys:count:from:] :220 (in CoreFoundation)
13 CoreFoundation 0x00000001857563f4 -[CFPrefsSource setValue:forKey:from:] :64 (in CoreFoundation)
14 CoreFoundation 0x00000001856bbfa4 __108-[_CFXPreferences(SearchListAdditions) withSearchListForIdentifier:container:cloudConfigurationURL:perform:]_block_invoke :260 (in CoreFoundation)
15 CoreFoundation 0x00000001856bb7c8 _normalizeQuintuplet :356 (in CoreFoundation)
16 CoreFoundation 0x00000001856bbe94 -[_CFXPreferences(SearchListAdditions) withSearchListForIdentifier:container:cloudConfigurationURL:perform:] :108 (in CoreFoundation)
17 CoreFoundation 0x000000018576008c -[_CFXPreferences setValue:forKey:appIdentifier:container:configurationURL:] :92 (in CoreFoundation)
18 CoreFoundation 0x000000018576370c _CFPreferencesSetAppValueWithContainer :128 (in CoreFoundation)
19 Foundation 0x000000018601bf80 -[NSUserDefaults(NSUserDefaults) setObject:forKey:] :68 (in Foundation)

最佳答案

您将对象转换为数据并尝试保存,您不能将可变对象直接保存到 NSUserDefaults

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self.recordUploadCnt];

[[NSUserDefaults standardUserDefaults] setObject:data forKey: KEY_SENSOR_UPLOAD];

[[NSUserDefaults standardUserDefaults] synchronize];

关于ios - NSUserDefaults setObject 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47586494/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com