在本地存储数据IOS [英] Store data locally IOS

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

问题描述

我有一个UIViewController供用户输入他们的数据并提交给我们的ASP.NET Webapi。大约有40个textField和其他控件。

I have a UIViewController for user to input their data and submit to our ASP.NET Webapi. there are about 40 textField and other controls.

有什么方法可以在本地保存输入。在用户单击提交按钮之前,数据将不会被清除。我希望数据仍在那里,甚至重启手机或重启程序。
以XML格式保存?

Is there any way i can save the input locally. the data will not be clear until user click the submit button. I want the data still there even restart the phone or restart the program. save in XML ?

推荐答案

您可以选择多种选项,其复杂程度如下:

There are quite a few options that you could choose from, ranging in complexity as follows:

  • Use an NSKeyedArchiver to save to the local filesystem. This involves making your model class conform to the NSCoding protocol.

使用sqlite和本地数据库。 C api可能非常冗长,但是有一个很好的Objective-C包装器叫做 FMDB

Use sqlite and a local database. The C api can be quite verbose, however there is a nice Objective-C wrapper called FMDB.

使用 CoreData 。这涉及创建本地数据模型(对象模式),然后指示CoreData将对象持久化到其存储。存储通常是一个sqlite数据库(包括ACID合规性 - 例如事务,原子性等),但CoreData也知道如何进行二进制和XML格式。

Use CoreData. This involves creating a local data-model (object schema) and then instructing CoreData to persist the object to its storage. Storage is typically an sqlite database (includes ACID compliance - eg transactions, atomicity, etc), but CoreData also knows how to do binary and XML formats.

无论您使用哪种方法,我建议您使用数据访问对象(DAO)设计模式,它为持久性方法提供协议。示例:

Whichever approach you use, I recommend using the Data Access Object (DAO) design pattern, which provides a protocol for persistence methods. Examples:

- (Customer*) findCustomByLastName:(NSString*)lastName
- (void) save:(Customer*)customer

。 。以这种方式,它可以从一种非常简单的持久性风格开始,测试你的整体架构如何集成到一个有凝聚力的应用程序,然后交换另一个更健壮的风格。 这是使用NSKeyedArchiver的文件系统DAO的示例

. . in this way its possible to start with a very simple style of persistence, to test how your overall architecture integrates into a cohesive app, and then swap in another more robust style later. Here's an example of a file-system DAO using NSKeyedArchiver.

其他方法


  • ActiveRecord 模式是数据访问对象的替代方案,并且有一些非常受欢迎且受到良好支持的库提供这与CoreData。使用ActiveRecord模式或DAO模式,但您可能应该避免仅在视图控制器中转储所有持久性代码! ;)

  • Nick Lockwood 创建了一个很好的实用工具,可以轻松使用NSKeyedArchiving

  • 对于简单的情况,您也可以使用 NSUserDefaults

  • 如果需要安全性,您可以保存到钥匙串。 API再次有点低级,但是有很好的包装器。 这是一个

  • 自NeXTSTEP日起, NSDictionary 提供了以各种格式将自身序列化到磁盘的方法。现代格式包括XML,JSON,二进制。 (并且有一些时髦的遗留格式,仍然可以读取,但不赞成写作。例如Xcode用来保存其project.pbxproj文件)。

  • The ActiveRecord pattern is an alternative to Data Access Object, and there are some very popular and well-supported libraries that provide this with CoreData. Use the ActiveRecord pattern or the DAO pattern, but probably you should avoid just dumping all of the persistence code in your view controller! ;)
  • Nick Lockwood created a nice utility to make working with NSKeyedArchiving easy.
  • For simple cases you could also use NSUserDefaults
  • If security is required, you could save to the keychain. Again the API is somewhat low-level, however there are nice wrappers. Here's one.
  • Since the NeXTSTEP days, an NSDictionary has provided methods to serialize itself to disk in various formats. Modern formats include XML, JSON, binary. (And there are some funky legacy formats, that can still be read, but are deprecated for writing. . such as the one Xcode uses to save its project.pbxproj file).

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

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