带有 NSDictionary 的数组不想保存到 standardUserDefaults

发布于 2024-09-27 08:26:06 字数 1406 浏览 9 评论 0原文

我遇到了一个......奇怪的问题。

我将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事还在继续 2024-10-04 08:26:06

您确定 defaults 不是 nil 吗?

Are you sure defaults isn't nil?

乄_柒ぐ汐 2024-10-04 08:26:06

您真的只是将 NSString 实例放入字典中吗?每次我看到 NSUserDefaults 保存不完整时,都是因为字典中的某些对象与 plist 格式不兼容。您只能在所有对象上存储 NSNumberNSStringNSArrayNSDictionaryNSData plist 格式的版本(有些也允许 NSDate)。

Are you really just putting NSString instances into the dictionary? Every time I've seen incomplete saving from NSUserDefaults, it's been because some of the objects in the dictionary aren't compatible with the plist format. You can only store NSNumber, NSString, NSArray, NSDictionary, NSData on all versions of the plist format (some allow NSDate too).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文