IOS Data Persistence: Using NSUserDefaults to store data

Source: Internet
Author: User

IOS Data Persistence: Using NSUserDefaults to store data

1. Usage:NSUserDefaults is used to store data similar to user configurations and program settings.

In iOS, you can use NSUserDefault, NSKeyedArchiver, sqlite, and CoreData to store data. NSUserDefaults is used to store data similar to user configurations, the latter two users store massive and complex data.

NSUserDefault supports all native data types such as NSString, NSNumber, NSDate, NSArray, NSDictionary, BOOL, NSInteger, and NSFloat.If you want to store custom objects (such as custom class objects), you must convert them to NSData storage.

2. Usage:

NSUserDefault is easy to use:

Class A page:

NSUserDefaults * userDefault = [NSUserDefaults standardUserDefaults];

[UserDefault setObject: @ "Beijing" forKey: LASTCITY];

[UserDefault synchronize];

Page B:

 

NSUserDefaults * userDefault = [NSUserDefaults standardUserDefaults];

NSString * lastCity = [userDefault objectForKey: LASTCITY];

3. Usage Details:

 

If you want to store custom objects (such as custom class objects), you must convert them to NSData storage:

 

[Cpp] 
  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];
    After adding data to NSUserDefaults, they become global variables, and the data in NSUserDefault can be read and written in the App:

     

     

    [Cpp]
    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]);

      To delete a data item, you can use removeObjectForKey to delete the data:

       

      [Cpp]
      1. [MySettingData removeObjectForKey: @ arrItem];

         

        It should be noted that NSUserDefaults regularly writes data in the cache to the disk, rather than writing data instantly. To prevent data loss caused by program exit after NSUserDefaults is written, you can use synchronize to force data to be written to the disk immediately after data is written:

         

         

        [Cpp]
        1. [MySettingData synchronize]; after running the preceding statement, the data in NSUserDefaults is written. in the plist file, if the program is run on the simulator, you can find a plist file named YOUR-USERNAME under the/Users/YOUR-APP-DIR/Library/Application Support/iPhone Simulator/4.1/Applications/YOUR-Bundle_Identifier.plist/Library/Prefereces directory for Mac, open the file with Xcode and you can see the data you just written.

          4. You can rewrite the object or data corresponding to the same keyword. After rewriting, the keyword corresponds to a new object or data, and the old object or data is automatically cleared.

Related Article

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.