带有 NSDictionary 的数组不想保存到 standardUserDefaults
我遇到了一个......奇怪的问题。
我将 NSDictionary 添加到 NSArray,然后将其保存为默认值。当我检查用户默认值上该键的对象计数时,它说计数为 0,但“内存上”的数组具有该对象(因此计数为 1)。
代码及其给出的输出是这样的:(
数组是一个 NSArray,默认值是 [NSUserDefaults standardUserDefaults])
- (void)addSongToQueue:(NSDictionary *)songData {
[array addObject:songData];
[defaults setObject:array forKey:@"OfflineArray"];
[defaults synchronize];
#if DEBUG
NSLog(@"Dictionary Data Received: %@", songData);
NSLog(@"Object Count on Array: %u", [array count]);
NSLog(@"Object Count on Defaults: %u", [[NSArray arrayWithArray:[defaults objectForKey:@"OfflineArray"]] count]);
[defaults removeObjectForKey:@"OfflineArray"];
[defaults synchronize];
#endif
}
这就是我如何调用它:
[className addSongToQueue:[NSDictionary dictionaryWithObjectsAndKeys:@"yadda", @"yadda", nil]];
这是输出:(我运行了两次,只是为了确保:P )
2010-10-11 21:47:35.807 AppName[1119:207] Dictionary Data Received: {
yadda = yadda;
}
2010-10-11 21:47:35.807 AppName[1119:207] Object Count on Array: 1
2010-10-11 21:47:35.808 AppName[1119:207] Object Count on Defaults: 0
2010-10-11 21:47:36.647 AppName[1119:207] Dictionary Data Received: {
yadda = yadda;
}
2010-10-11 21:47:36.647 AppName[1119:207] Object Count on Array: 2
2010-10-11 21:47:36.648 AppName[1119:207] Object Count on Defaults: 0
所以是的。我无法解决它。 :/
提前致谢。 :)
I am having a... strange problem.
I am adding an NSDictionary to an NSArray, then saving it to the defaults. When I check the object count of that key on the user defaults, it says the count is 0, but the array that's "on memory" has that object (so the count is 1).
The code, along with the output it gives is this:
(array is an NSArray, defaults is [NSUserDefaults standardUserDefaults])
- (void)addSongToQueue:(NSDictionary *)songData {
[array addObject:songData];
[defaults setObject:array forKey:@"OfflineArray"];
[defaults synchronize];
#if DEBUG
NSLog(@"Dictionary Data Received: %@", songData);
NSLog(@"Object Count on Array: %u", [array count]);
NSLog(@"Object Count on Defaults: %u", [[NSArray arrayWithArray:[defaults objectForKey:@"OfflineArray"]] count]);
[defaults removeObjectForKey:@"OfflineArray"];
[defaults synchronize];
#endif
}
This is how I am calling it:
[className addSongToQueue:[NSDictionary dictionaryWithObjectsAndKeys:@"yadda", @"yadda", nil]];
This is the output: (I ran it twice, just to make sure :P)
2010-10-11 21:47:35.807 AppName[1119:207] Dictionary Data Received: {
yadda = yadda;
}
2010-10-11 21:47:35.807 AppName[1119:207] Object Count on Array: 1
2010-10-11 21:47:35.808 AppName[1119:207] Object Count on Defaults: 0
2010-10-11 21:47:36.647 AppName[1119:207] Dictionary Data Received: {
yadda = yadda;
}
2010-10-11 21:47:36.647 AppName[1119:207] Object Count on Array: 2
2010-10-11 21:47:36.648 AppName[1119:207] Object Count on Defaults: 0
So yeah. I can't be able to solve it. :/
Thanks in advance. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您确定
defaults
不是 nil 吗?Are you sure
defaults
isn't nil?您真的只是将
NSString
实例放入字典中吗?每次我看到NSUserDefaults
保存不完整时,都是因为字典中的某些对象与 plist 格式不兼容。您只能在所有对象上存储NSNumber
、NSString
、NSArray
、NSDictionary
、NSData
plist 格式的版本(有些也允许NSDate
)。Are you really just putting
NSString
instances into the dictionary? Every time I've seen incomplete saving fromNSUserDefaults
, it's been because some of the objects in the dictionary aren't compatible with the plist format. You can only storeNSNumber
,NSString
,NSArray
,NSDictionary
,NSData
on all versions of the plist format (some allowNSDate
too).