How to store user data iOS

前端 未结 3 1835
情歌与酒
情歌与酒 2020-12-06 08:38

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 NSUserDe

相关标签:
3条回答
  • 2020-12-06 09:03

    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.

    0 讨论(0)
  • 2020-12-06 09:06

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

    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?

    0 讨论(0)
  • 2020-12-06 09:14

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

    • 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.

    • 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.

    • 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.

    • UIDocument: A full-featured starting point for managing user data, including syncing with 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.

    • 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.

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

    • 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.

    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:

    // writing
    [myArray writeToFile:somePath atomically:YES];
    
    // reading
    myArray = [[NSArray arrayWithContentsOfFile:somePath] mutableCopy];
    
    0 讨论(0)
提交回复
热议问题