存储数组中的 NSNumbers 消失

发布于 2024-09-06 01:18:27 字数 432 浏览 4 评论 0原文

因此,我将 NSNumber 对象的 NSMutableArray 存储到文件“times.plist”中,然后加载它并在启动时保留它,NSLog 显示正确的值,但后来 [times count] 等于 0。为什么 NSNumber 消失了?

times = [[NSMutableArray alloc] initWithObjects:[[NSNumber alloc] initWithFloat:time],nil];
...

[times writeToFile:@"times.plist" atomically:YES];
...

times = [[NSMutableArray arrayWithContentsOfFile:@"times.plist"] retain];
NSLog(@"%f",[[times objectAtIndex:0] floatValue]);

So I stored an NSMutableArray of NSNumber objects into the file "times.plist" then I load it and retain it on launch and the NSLog shows the correct value, but later the [times count] equals 0. Why are the NSNumbers disappearing?

times = [[NSMutableArray alloc] initWithObjects:[[NSNumber alloc] initWithFloat:time],nil];
...

[times writeToFile:@"times.plist" atomically:YES];
...

times = [[NSMutableArray arrayWithContentsOfFile:@"times.plist"] retain];
NSLog(@"%f",[[times objectAtIndex:0] floatValue]);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

酸甜透明夹心 2024-09-13 01:18:27

好吧,笨笨笨笨。永远不要做我做过的事。在我的-init中;方法我已经用代码启动了我的时间数组:

times = [[NSMutableArray alloc] initWithObjects:nil];

并且我试图用以下方法恢复它们:

times = [[NSMutableArray arrayWithContentsOfFile:@"times.plist"] retain];

但是我在启动它之前恢复它并且每次都会删除它。从我的错误中吸取教训并检查你的 -init; 的顺序方法。总是。

Okay DUMB DUMB DUMB. Never do what I did. in my -init; method i had already initiated my times array with the code:

times = [[NSMutableArray alloc] initWithObjects:nil];

and I was trying to restore them with:

times = [[NSMutableArray arrayWithContentsOfFile:@"times.plist"] retain];

BUT I was restoring it BEFORE I was initiating it and that erased it every time. Learn from my mistake and check the order of your -init; methods. ALWAYS.

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