无法在 NSUserDefaults 中保存 NSMutableArray

发布于 2024-12-21 21:36:04 字数 781 浏览 1 评论 0原文

我试图在设备上保存 NSMutableArray ,但每次重新加载应用程序时,数据都会被删除。

我尝试使用 NSUserDefaults 但问题仍然存在,

这是我正在使用的代码

-(void) addingData{

    [data addObject:theBarcodeString]; //data is the NSMutableArray

    [mainTableView reloadData]; //this is a UITableView where the data from the NSMutableArray is populated
    [self endText];
    history=[NSUserDefaults standardUserDefaults];
    [history        setObject:data          forKey:@"history"  ];
}

,这是我加载数据的位置,

- (void)viewDidLoad
{
    ...
    if (data == nil) {
        data = [[NSMutableArray alloc] init]; //onload it always goes to his line

    else {

        data=[[NSUserDefaults standardUserDefaults] objectForKey:@"history"  ];
    }
    ...
}

我做错了什么?

I am trying to save an NSMutableArray on device , but everytime I reload the application the data is erased.

I tried to use NSUserDefaults but the problem persists

here is the code I am using

-(void) addingData{

    [data addObject:theBarcodeString]; //data is the NSMutableArray

    [mainTableView reloadData]; //this is a UITableView where the data from the NSMutableArray is populated
    [self endText];
    history=[NSUserDefaults standardUserDefaults];
    [history        setObject:data          forKey:@"history"  ];
}

and here is where I load the data

- (void)viewDidLoad
{
    ...
    if (data == nil) {
        data = [[NSMutableArray alloc] init]; //onload it always goes to his line

    else {

        data=[[NSUserDefaults standardUserDefaults] objectForKey:@"history"  ];
    }
    ...
}

what am I doing wrong ?

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

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

发布评论

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

评论(2

雾里花 2024-12-28 21:36:04

您不应该测试 data == nil 是否。相反,只需执行以下操作:

data = [[NSArray alloc] initWithArray:[[NSUserDefaults standardUserDefaults] 
    objectForKey:@"history"]];

请注意,您必须稍后发布它。

如果您希望 data 成为可变数组,则必须使用:

data = [[NSUserDefaults standardUserDefaults] mutableArrayValueForKey:@"history"];

如果您尝试在不使用 mutableArrayValueForKey 的情况下对数据执行变异操作,您将会崩溃,因为所有对象从 NSUserDefaults 返回的值是不可变的。

You shouldn't be testing to see if data == nil. Instead, just do:

data = [[NSArray alloc] initWithArray:[[NSUserDefaults standardUserDefaults] 
    objectForKey:@"history"]];

Note that you will have to release that later.

If you want data to be a mutable array, you must use:

data = [[NSUserDefaults standardUserDefaults] mutableArrayValueForKey:@"history"];

If you try to performing mutating operations on data without using mutableArrayValueForKey, you will get a crash, because all objects returned from NSUserDefaults are immutable.

悲歌长辞 2024-12-28 21:36:04

同步将对持久域的任何修改写入磁盘
并将所有未修改的持久域更新为磁盘上的内容。

  • (BOOL)同步

如果数据成功保存到磁盘,则返回YES,否则
没有。

因此,您必须添加 [history Synchronize] 来强制写入默认值

编辑:来自 文档

因为该方法会定期自动调用,
仅当您无法等待自动执行时才使用此方法
同步(例如,如果您的应用程序即将退出)或
如果您想将用户默认值更新为磁盘上的内容,即使
您尚未进行任何更改。

因此,如果您想立即保存更改,请调用[历史同步]

synchronize Writes any modifications to the persistent domains to disk
and updates all unmodified persistent domains to what is on disk.

  • (BOOL)synchronize

Return Value YES if the data was saved successfully to disk, otherwise
NO.

So you have to add [history synchronize] to force write on the defaults

Edit: From Docs

Because this method is automatically invoked at periodic intervals,
use this method only if you cannot wait for the automatic
synchronization (for example, if your application is about to exit) or
if you want to update the user defaults to what is on disk even though
you have not made any changes.

So if you want to save changes immediately call [history synchronize].

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