IOS data persistence using Nsuserdefaults to store data

Source: Internet
Author: User

1. Use: Nsuserdefaults is used to store data such as user-like configuration, program settings information , etc.

nsuserdefault, Nskeyedarchiver, SQLite, CoreData a few common ways to store data, Where Nsuserdefaults is used to store data such as a user-like configuration, both users store large quantities and more complex data.

nsuserdefault basically supports all native data types nsstring, NSNumber, NSDate, NSArray, Nsdictionary If you want to store a custom object, such as a custom class object, you must convert it to nsdata storage.

2. Usage:

The use of Nsuserdefault is relatively simple:

Category A page:

nsuserdefaults *userdefault = [nsuserdefaultsstandarduserdefaults] ;

[Userdefault setobject: @ "Beijing" forkey:lastcity];

[Userdefault synchronize];

Class B page:

nsuserdefaults *userdefault = [nsuserdefaultsstandarduserdefaults];

nsstring *lastcity = [Userdefault objectforkey:lastcity];

3. Detail usage:

If you want to store a custom object, such as a custom class object, you must convert it to nsdata storage:

[CPP]View Plaincopy
  1. NSArray  *arr = [[nsarray alloc] initwithobjects:@ " arr1 " , @ " arr2 " , NIL]  
  2. [mysettingdata setobject:arr forkey:@" Arritem " ];  
  3. [mysettingdata setobject:@" admin "  forkey:@ " user_name " ];  
  4. [mysettingdata setbool: @YES  forkey:@" Auto_login " ];  
  5. [Mysettingdata setinteger:1 forkey:@"Count"];

when you add data to nsuserdefaults, they become global variables, and the app can read and write data from the Nsuserdefault:

[CPP]View Plaincopy
  1. Nsuserdefaults *mysettingdatar = [Nsuserdefaults standarduserdefaults];
  2. nslog (@" arritem=%@ " , [mysettingdatar objectforkey:@ " Arritem "
  3. nslog (@" user_name=%@ " , [mysettingdatar objectforkey:@ " user_name "
  4. NSLog (@"count=%d", [Mysettingdatar integerforkey:@"Count"]);

If you want to delete a data item, you can use Removeobjectforkey to delete the data:

[CPP]View Plaincopy
    1. [mysettingdata removeobjectforkey:@"Arritem"];

It is important to note that the nsuserdefaults is timed to write the data in the cache to the disk, rather than the instant write, in order to prevent the data loss caused by the program exit after writing the nsuserdefaults. You can use synchronize to force the data to be written to disk immediately after the data is written:

[CPP]View Plaincopy
    1. [ mysettingdata synchronize];  
After you run the above statement, the data in the Nsuserdefaults is written to the. plist file, and if you run the program on the emulator, you can/users/your-username/library/application the Mac Support/iphone simulator/4.1/applications/your-app-dir/library/prefereces directory below find a file named Your-bundle_ Identifier.plist the plist file, open the file with Xcode and you can see the data you just wrote.

4, for the same keyword corresponding to the object or data, it can be rewritten, after rewriting the keyword corresponding to the new object or data, the old object or data will be automatically cleaned up.

IOS data persistence using Nsuserdefaults to store data

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.