iOS nsuserdefaults store data (preferences)

Source: Internet
Author: User

iOS nsuserdefaults store data (preferences)

The 1.NSUserDefaults is used to store data with small amounts of data, primarily user-configurable, but can also support storing some small data
Includes:nsstring, NSNumber, NSDate, Nsarray, Nsdictionary
2. The data is stored in a plist file. This file is located in the/library/preferences directory of the app sibling directory

3.NSUserDefaults is mainly setobject and object Forkey, big data use other storage, such as sqlite3,coredate, etc.;

4. After saving the data using the preferences, the time it saves to the system is indeterminate, the data will be saved to the Preferences folder automatically at a certain point in the future, and if you need to store the data immediately, you can use [defaults synchronize];

1. plist file storage address for preference settings

NSString *path = [Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) lastObject];   NSLog (@ "%@", Path);

2. Read deposit NSString (nsnumber,nsdate)

Nsuserdefaults *defaults =[Nsuserdefaults Standarduserdefaults]; //Write Data[Defaults setobject:@"teststring"Forkey:@"test1"]; //Write Now[Defaults synchronize]; //reading DataNSString *STR = [Defaults objectforkey:@"test1"]; NSLog (@"---%@", str);

3. Read Deposit Nsarray,nsdictionary

Nsarray,nsdictionary are immutable types and are similar.

However, if the deposit is nsmutablearray,nsmutabledictionary, the read-out array is immutable in the type: Nscfarray. needs to be transformed into Nsmutablearray.

Nsuserdefaults *defaults =[Nsuserdefaults Standarduserdefaults]; Nsmutablearray*array = [Nsmutablearray arraywithobjects:@"test1",@"test2",@"test3", nil]; [Array AddObject:@"test4"]; //Write Data[Defaults Setobject:array Forkey:@"test2"]; //Write Now[Defaults synchronize]; //reading Data//reason: '-[__nscfarray insertobject:atindex:]: Mutating method sent to immutable object ' this directly equals the word mutablearray type NS Cfarray, cannot continue to add to the array//Nsmutablearray *mutablearray = [Defaults objectforkey:@ "Test2"];Nsmutablearray *mutablearray = [Nsmutablearray arraywitharray:[defaults objectforkey:@"test2"]]; [Mutablearray AddObject:@"Test5"]; NSLog (@"%@", Mutablearray);

iOS nsuserdefaults store data (preferences)

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.