如何存储用户数据iOS

时间:2021-11-15 12:41:32

I need to store data that the user can add, they can add an unlimited amount. They can either be NSStrings or UIImages. I have looked into NSUserDefaults but it seems that it is used for small amounts of data such as settings or preferences.

我需要存储用户可以添加的数据,他们可以添加无限的数量。它们可以是nsstring或uiimage。我已经研究了NSUserDefaults,但它似乎用于像设置或偏好这样的少量数据。

What would be the best/most secure way to store the users information so that when they close the app it is still in the app. The data populates a UITableView and is a NSMutableArray.

什么是最好/最安全的方式来存储用户信息,以便当他们关闭应用程序时,它仍然在应用程序中。数据填充UITableView,并且是NSMutableArray。

Whats the best way to do this?

最好的办法是什么?

3 个解决方案

#1


1  

You should use Core Data. There is a very good, free beginners course online avaibable called cs193p, see here http://www.stanford.edu/class/cs193p/cgi-bin/drupal/node/287, it is also available through iTunes U. It's really worth the time to watch and easy understandable.

您应该使用核心数据。有一个非常好的,免费的初学者在线课程,叫做cs193p,在这里可以看到http://www.stanford.edu/class/cs193p/cgi-bin/drupal/node/287,它也可以通过iTunes u获得。

#2


11  

There must be a dozen ways to store user data in iOS. Here are several:

在iOS中存储用户数据的方式必须有十多种。这里有几个:

  • Property lists: An easy way to store a graph of common data storage objects and containers. This is a good place to start if you're just learning the iOS ropes.

    属性列表:存储公共数据存储对象和容器的图形的简单方法。这是一个很好的开始,如果你只是学习iOS的绳索。

  • NSKeyedArchiver and NSKeyedUnarchiver: Provides an easy way to serialize and deserialize your objects to/from a chunk of data, which you can then write/read using NSData's methods.

    NSKeyedArchiver和NSKeyedUnarchiver:提供了一种将对象序列化和反序列化到/从数据块的简单方法,然后可以使用NSData的方法编写/读取数据块。

  • NSFileHandle: Read and write data in whatever format you like using a nice Objective-C API. More generally, you should read up on the iOS file system.

    NSFileHandle:使用一个不错的Objective-C API来读取和写入数据。更一般地说,您应该阅读iOS文件系统。

  • UIDocument: A full-featured starting point for managing user data, including syncing with iCloud.

    UIDocument:管理用户数据的功能齐全的起点,包括与iCloud同步。

  • Keychain: Not a general purpose data storage mechanism, but if you're storing sensitive items like passwords, credit card numbers, etc., you should use the keychain API.

    密钥链:不是一种通用的数据存储机制,但是如果要存储密码、信用卡号等敏感项,应该使用密钥链API。

  • POSIX file API: Good old C file handles with the read and write functions you learned in college, if you went to college before Java was a thing.

    POSIX文件API:如果您在Java还没有出现之前就上过大学,那么可以使用大学时学到的读写函数来处理旧的C文件句柄。

  • SQLite: According to the web site: "SQLite is the most widely deployed SQL database engine in the world."

    SQLite:网站上说:“SQLite是世界上部署最广泛的SQL数据库引擎。”

  • Core Data: A powerful (but also somewhat complex object graph manager. This is a good choice if you have many different pieces of related data to store.

    核心数据:一个强大的(但也有些复杂的对象图管理器)。如果要存储许多不同的相关数据,这是一个很好的选择。

What would be the best/most secure way to store the users information so that when they close the app it is still in the app. The data populates a UITableView and is a NSMutableArray.

什么是最好/最安全的方式来存储用户信息,以便当他们关闭应用程序时,它仍然在应用程序中。数据填充UITableView,并且是NSMutableArray。

Best is subjective -- you'll need to consider your needs and look at the various options. For many people, though, best means least painful or easiest to learn. As mentioned above, property lists may be the way to go in that case. If your array contains simple data (strings, data, dates, numbers) in standard containers (arrays or dictionaries), your file I/O can be as simple as something like this:

最佳是主观的——你需要考虑自己的需求,考虑各种选择。然而,对许多人来说,最好意味着最不痛苦或最容易学习。如上所述,属性列表可能是这种情况下的方法。如果您的数组在标准容器(数组或字典)中包含简单的数据(字符串、数据、日期、数字),那么文件I/O可以像这样简单:

// writing
[myArray writeToFile:somePath atomically:YES];

// reading
myArray = [[NSArray arrayWithContentsOfFile:somePath] mutableCopy];

#3


0  

If you have only some array you can check plist. is verry simple and powerful.

如果您只有一些数组,您可以检查plist。是简单而有力的。

https://developer.apple.com/library/iOs/documentation/General/Reference/InfoPlistKeyReference/Articles/AboutInformationPropertyListFiles.html

https://developer.apple.com/library/iOs/documentation/General/Reference/InfoPlistKeyReference/Articles/AboutInformationPropertyListFiles.html

This is a great resources: Should I use NSUserDefaults or a plist to store data?

这是一个很好的资源:我应该使用NSUserDefaults还是plist来存储数据?

#1


1  

You should use Core Data. There is a very good, free beginners course online avaibable called cs193p, see here http://www.stanford.edu/class/cs193p/cgi-bin/drupal/node/287, it is also available through iTunes U. It's really worth the time to watch and easy understandable.

您应该使用核心数据。有一个非常好的,免费的初学者在线课程,叫做cs193p,在这里可以看到http://www.stanford.edu/class/cs193p/cgi-bin/drupal/node/287,它也可以通过iTunes u获得。

#2


11  

There must be a dozen ways to store user data in iOS. Here are several:

在iOS中存储用户数据的方式必须有十多种。这里有几个:

  • Property lists: An easy way to store a graph of common data storage objects and containers. This is a good place to start if you're just learning the iOS ropes.

    属性列表:存储公共数据存储对象和容器的图形的简单方法。这是一个很好的开始,如果你只是学习iOS的绳索。

  • NSKeyedArchiver and NSKeyedUnarchiver: Provides an easy way to serialize and deserialize your objects to/from a chunk of data, which you can then write/read using NSData's methods.

    NSKeyedArchiver和NSKeyedUnarchiver:提供了一种将对象序列化和反序列化到/从数据块的简单方法,然后可以使用NSData的方法编写/读取数据块。

  • NSFileHandle: Read and write data in whatever format you like using a nice Objective-C API. More generally, you should read up on the iOS file system.

    NSFileHandle:使用一个不错的Objective-C API来读取和写入数据。更一般地说,您应该阅读iOS文件系统。

  • UIDocument: A full-featured starting point for managing user data, including syncing with iCloud.

    UIDocument:管理用户数据的功能齐全的起点,包括与iCloud同步。

  • Keychain: Not a general purpose data storage mechanism, but if you're storing sensitive items like passwords, credit card numbers, etc., you should use the keychain API.

    密钥链:不是一种通用的数据存储机制,但是如果要存储密码、信用卡号等敏感项,应该使用密钥链API。

  • POSIX file API: Good old C file handles with the read and write functions you learned in college, if you went to college before Java was a thing.

    POSIX文件API:如果您在Java还没有出现之前就上过大学,那么可以使用大学时学到的读写函数来处理旧的C文件句柄。

  • SQLite: According to the web site: "SQLite is the most widely deployed SQL database engine in the world."

    SQLite:网站上说:“SQLite是世界上部署最广泛的SQL数据库引擎。”

  • Core Data: A powerful (but also somewhat complex object graph manager. This is a good choice if you have many different pieces of related data to store.

    核心数据:一个强大的(但也有些复杂的对象图管理器)。如果要存储许多不同的相关数据,这是一个很好的选择。

What would be the best/most secure way to store the users information so that when they close the app it is still in the app. The data populates a UITableView and is a NSMutableArray.

什么是最好/最安全的方式来存储用户信息,以便当他们关闭应用程序时,它仍然在应用程序中。数据填充UITableView,并且是NSMutableArray。

Best is subjective -- you'll need to consider your needs and look at the various options. For many people, though, best means least painful or easiest to learn. As mentioned above, property lists may be the way to go in that case. If your array contains simple data (strings, data, dates, numbers) in standard containers (arrays or dictionaries), your file I/O can be as simple as something like this:

最佳是主观的——你需要考虑自己的需求,考虑各种选择。然而,对许多人来说,最好意味着最不痛苦或最容易学习。如上所述,属性列表可能是这种情况下的方法。如果您的数组在标准容器(数组或字典)中包含简单的数据(字符串、数据、日期、数字),那么文件I/O可以像这样简单:

// writing
[myArray writeToFile:somePath atomically:YES];

// reading
myArray = [[NSArray arrayWithContentsOfFile:somePath] mutableCopy];

#3


0  

If you have only some array you can check plist. is verry simple and powerful.

如果您只有一些数组,您可以检查plist。是简单而有力的。

https://developer.apple.com/library/iOs/documentation/General/Reference/InfoPlistKeyReference/Articles/AboutInformationPropertyListFiles.html

https://developer.apple.com/library/iOs/documentation/General/Reference/InfoPlistKeyReference/Articles/AboutInformationPropertyListFiles.html

This is a great resources: Should I use NSUserDefaults or a plist to store data?

这是一个很好的资源:我应该使用NSUserDefaults还是plist来存储数据?