Li Hongqiang data storage for iOS development

Source: Internet
Author: User

Li Hongqiang data storage for iOS development

  Common ways for iOS app data storage

1.lXML attribute list (plist) archive2.lPreference (preference setting)3.lNSKeyedArchiver Archive (nscoding)4.lsqlite35.lCore Data Documents:saves data that needs to be persisted when the app is run, and the directory is backed up when itunes synchronizes the device. For example, a game app can save a game archive in that directorytmp:Save the temporary data required for the app to run, and then delete the corresponding file from the directory when you are finished using it. When the app is not running, the system may also purge files in that directory. itunes does not back up this directory when syncing the devicelibrary/caches:saves data that needs to be persisted when the app runs, and itunes syncs the device without backing up the directory. Non-critical data with large storage volumes and no backup requiredlibrary/preference:Save all of your app's preferences, and the iOS Settings app will find the app's settings in that directory. This directory is backed up when itunes syncs the device common ways to get the sandboxed catalog applied  using the Nssearchpathfordirectoriesindomains function

Nsarray *array = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, NO); Nsuserdomainmask represents from the user folder, YES represents the wavy character "~" in the expanded path

NSString *documents = [array objectatindex:0]; In iOS, only one directory matches the parameters passed in, so there is only one element in the collection

Preferences : Cannot store custom objects, generally store user's account password information

1. Deposit Data

Nsuserdefaults *defaults = [Nsuserdefaults standarduserdefaults];

[Defaults setobject:@ "Itcast" forkey:@ "username"];

[Defaults setfloat:18.0f forkey:@ "Text_size"];

[Defaults setbool:yes forkey:@ "Auto_login"];

2. Reading data

Nsuserdefaults *defaults = [Nsuserdefaults standarduserdefaults];

NSString *username = [Defaults stringforkey:@ "username"];

float textSize = [Defaults floatforkey:@ "Text_size"];

BOOL autologin = [Defaults boolforkey:@ "Auto_login"];

[Defaults synchornize]; In order for the program to write immediately to call this method

  nskeyedarchiver: Need to comply with <NSCoding> agreement

Implement the following two methods

1. Encodewithcoder: This method is called every time the object is archived. In this method, you typically specify how to archive each instance variable in an object, and you can use the Encodeobject:forkey: method to archive instance variables2. Initwithcoder: This method is called every time the object is recovered (decoded) from the file. In this method, you typically specify how to decode the data in a file as an instance variable of an object, and you can use the Decodeobject:forkey method to decode the instance variable

1. Deposit Data

[Nskeyedarchiver Archiverootobject:array Tofile:path];

2. Reading data

[Nskeyedunarchiver Unarchiveobjectwithfile:path];

Li Hongqiang data storage for iOS development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.