简体   繁体   English

如何将NSArray存储到plist或数据库中?

[英]How to store a NSArray into plist or database?

I have this app which has to store information when I hit some button.. The fact is, when I hit this button I need to store this data locally so I can take this in other view which is going to load it at show it as data. 我有这个应用程序,当我点击一些按钮时,它必须存储信息..事实是,当我点击这个按钮时,我需要在本地存储这些数据,所以我可以在其他视图中将其加载到显示为数据。

I'm thinking in some plis or database. 我正在考虑一些plis或数据库。 The problem is I can't get to an idea or something good. 问题是我无法得到一个好主意或好事。

  1. I pass the array with objects. 我用对象传递数组。
  2. I have the button. 我有按钮。

Which is the best way to store the entire array inside the phone, so I can read it as the same way it is stored? 哪个是将整个阵列存储在手机内的最佳方式,所以我可以像存储它一样读取它?

If this is not clear, please tell me. 如果不清楚,请告诉我。

Thanks in advance 提前致谢

Easies way is to save it to a plist 简单的方法是将它保存为plist

Like this: 像这样:

//create array
NSMutableArray *arr = [[NSMutableArray alloc] init];

//Add data
[arr addObject:@"some object"];

//String Path of file
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];

//Save
[arr writeToFile:path atomically:YES];

Loading it later like this: 像这样加载它:

//Load it
NSMutableArray *arr = [[NSMutableArray alloc] initWithContentsOfFile:path];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM