在 iphone 上本地存储数据 [英] storing data locally on the iphone

查看:33
本文介绍了在 iphone 上本地存储数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个应用程序,我想在设备上本地存储用户信息,而不使用任何服务器数据库 - 但所有内容都在设备端.我希望存储特定的用户位置并将其显示在表格视图中,因此即使用户稍后启动应用程序 - 我也可以提取历史记录并将过去的位置提供给历史记录表.基本上是来自本地数据库的读/写功能.

I am building an app where I'd like to store user information locally on the device, without using any server database - but everything on the device side. I am looking to store specific user locations and show it in a table view, and so even when the user launches the app later - I can pull the history and feed the history table with the past locations. Basically a read/write capability from a local database.

现在,我知道以前有很多类似的问题,但我找不到解决在没有外部数据库的情况下在本地保存数据的问题.例如,我不确定在这里使用 Core Data 是否正确且最简单.

Now, I know that there were many questions like that before but I could not find one that addresses saving data locally without an external data base. I am not sure, for example, that using Core Data is the correct and simplest thing to do here.

希望得到任何建议.

推荐答案

对于简单数据,您应该使用 NSUserDefaults.CoreData 非常酷但主要用于存储数据库结构,并引入了复杂性(但我喜欢它:)).如果你只需要存储字符串、数组等等(基本上是首选项),你可以使用 NSUserDefaults:

For simple data you should use NSUserDefaults. CoreData is very cool but mainly to store DB structures, and introduces complexity (but i love it:)). If you just need to store String, Array and so on (basically prefs), you can go with NSUserDefaults:

例如:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];  //load NSUserDefaults
NSArray *fakeFavs = [[ NSArray alloc] initWithObjects:@"2",@"4", @"100", nil];  //declare array to be stored in NSUserDefaults
[prefs setObject:fakeFavs forKey:@"favourites"];  //set the prev Array for key value "favourites"

这篇关于在 iphone 上本地存储数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆