plist 作为持久存储,用于在关闭和重新启动应用程序之间存储和检索数据

发布于 2024-10-02 23:52:22 字数 1985 浏览 3 评论 0原文

当用户第一次启动应用程序时,我试图将一些属性保存到 plist 中,然后当用户重新启动应用程序时,我必须检索这些数据并将其显示给用户,这样我就没有再次请求相同的数据。

我猜这应该是非常简单的,但可能我遗漏了一些东西。

如果有人可以向我指出执行相同操作的示例代码,那就太好了,如果代码能够保存和检索从字典到 plist 以及从 plist 到字典的数据,那就太好了。

我查看了 Apple 属性列表编程指南,并使用了读取和写入属性列表数据中的代码

http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/PropertyLists/ReadWritePlistData/ReadWritePlistData.html #//apple_ref/doc/uid/10000048i-CH8-SW1

但是,当我重新启动应用程序时,我无法取回已存储的数据。

*编辑*

我添加了一些代码,以便有人可以指出我是否犯了任何错误。

我在项目资源文件夹中创建了一个 plist 文件“Data.plist”。

这就是我在视图控制器之一中保存数据的方式

    NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"Data.plist"];
//NSString *error;

NSMutableDictionary *dict = [NSMutableDictionary dictionary];


[dict setObject:@"xxxxxx" forKey:@"phoneNumber"];
[dict setObject:@"password" forKey:@"password"];
[dict setObject:@"email" forKey:@"email"];

[dict writeToFile:finalPath atomically:NO];

这是我在同一视图控制器 viewDidLoad 方法上读取数据的方式,因为我想在下次应用程序启动时加载数据。

    NSDictionary *dictionary;

// read "Data.plist" from application bundle
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"Data.plist"];
dictionary = [NSDictionary dictionaryWithContentsOfFile:finalPath];

// dump the contents of the dictionary to the console
for (id key in dictionary) {
    NSLog(@"bundle: key=%@, value=%@", key, [dictionary objectForKey:key]);
}   

在保存到文件之前,我可以在 gdb 上执行“po dict”,并且可以看到文件中有数据

NSLog 打印以下错误

断言失败:(cls),函数 getName,文件 /SourceCache/objc4_Sim/objc4-427.1.1/runtime /objc-runtime-new.m,第 3939 行。

因为当我加载数据并在 gdb 上执行“po 字典”时,它是空的

I am trying to save few property into the plist when the user starts the app for the first time, and then I have to retrieve those data and show it to the user, when the user restart the app, so that I don't have to ask for the same data again.

I am guessing this should be very straight forward, but may be I am missing something.

It would be nice if some one can point me to a sample code for doing the same thing, it would be nice if the code saves and retrieves the data from dictionary to plist and from plist to dictionary.

I looked into the Apple Property List Programming guide and used the code from Reading and Writing Property list data

http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/PropertyLists/ReadWritePlistData/ReadWritePlistData.html#//apple_ref/doc/uid/10000048i-CH8-SW1

but, when i restart my app, I don't get back the data that I have stored.

*EDIT*

I am adding some code so that some one can point out if I am making any mistake.

I created a plist file "Data.plist" in my project Resources folder.

This is how I save data in one of my view controller

    NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"Data.plist"];
//NSString *error;

NSMutableDictionary *dict = [NSMutableDictionary dictionary];


[dict setObject:@"xxxxxx" forKey:@"phoneNumber"];
[dict setObject:@"password" forKey:@"password"];
[dict setObject:@"email" forKey:@"email"];

[dict writeToFile:finalPath atomically:NO];

This is how I am reading the the data on the same view controller viewDidLoad method, because I want to load the data from when next time app starts.

    NSDictionary *dictionary;

// read "Data.plist" from application bundle
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"Data.plist"];
dictionary = [NSDictionary dictionaryWithContentsOfFile:finalPath];

// dump the contents of the dictionary to the console
for (id key in dictionary) {
    NSLog(@"bundle: key=%@, value=%@", key, [dictionary objectForKey:key]);
}   

Before saving to the file i can do "po dict" at gdb and can see there are data in the file

NSLog print following error

Assertion failed: (cls), function getName, file /SourceCache/objc4_Sim/objc4-427.1.1/runtime/objc-runtime-new.m, line 3939.

Because when i load data and do "po dictionary" at gdb it is empty

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

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

发布评论

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

评论(1

何处潇湘 2024-10-09 23:52:22

-[NSDictionary writeToFile:atomically:] 返回 YESNO 取决于它是否成功。你检查返回值吗?

如果我不得不猜测,我会说这是因为您正在将文件写入应用程序包中。您应该将其写入文件系统中的其他位置; ~/Library/Application Support/ 是通常的位置。

编辑:

您可以使用以下命令检查返回值:

BOOL success = [dict writeToFile:finalPath atomically:YES];
if (!success) {
    NSLog(@"Could not write file");
}

-[NSDictionary writeToFile:atomically:] returns YES or NO depending on whether it's successful or not. Are you checking the return value?

If I had to take a guess, I'd say it's because you're writing the file into the application bundle. You should write it elsewhere in the file system; ~/Library/Application Support/<application name> is the usual location.

Edit:

You can check the return value with something like:

BOOL success = [dict writeToFile:finalPath atomically:YES];
if (!success) {
    NSLog(@"Could not write file");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文