plist 作为持久存储,用于在关闭和重新启动应用程序之间存储和检索数据
当用户第一次启动应用程序时,我试图将一些属性保存到 plist 中,然后当用户重新启动应用程序时,我必须检索这些数据并将其显示给用户,这样我就没有再次请求相同的数据。
我猜这应该是非常简单的,但可能我遗漏了一些东西。
如果有人可以向我指出执行相同操作的示例代码,那就太好了,如果代码能够保存和检索从字典到 plist 以及从 plist 到字典的数据,那就太好了。
我查看了 Apple 属性列表编程指南,并使用了读取和写入属性列表数据中的代码
但是,当我重新启动应用程序时,我无法取回已存储的数据。
*编辑*
我添加了一些代码,以便有人可以指出我是否犯了任何错误。
我在项目资源文件夹中创建了一个 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
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
-[NSDictionary writeToFile:atomically:]
返回YES
或NO
取决于它是否成功。你检查返回值吗?如果我不得不猜测,我会说这是因为您正在将文件写入应用程序包中。您应该将其写入文件系统中的其他位置;
~/Library/Application Support/
是通常的位置。编辑:
您可以使用以下命令检查返回值:
-[NSDictionary writeToFile:atomically:]
returnsYES
orNO
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: