Nsuserdefaults of iOS data storage

Source: Internet
Author: User

Objective:

As an Android developer, you must have heard of sharedpreferences, and then become an iOS development engineer. I don't know nsuserdefaults! Now let's get to know each other.

Nsuserdefaults Introduction:

Nsuserdefaults is a kind of storage of lightweight local data, Nsuserdefaults is a single case, mainly for the permanent preservation of data, simple and practical. Trial scenario: For example, we save the user's login information, a logo, and so on. Supported data formats are:nsnumber (Integer, Float, Double), Nsstring,nsdate,nsarray,nsdictionary,bool, and other system-defined data types . The above marked red is to attract everyone's attention, writing a test program when you write a nsmutablearray and then save, unfortunately, is directly hanging off, and later checked the information, the original nsuserdefaults storage objects are immutable!

Don't talk a lot about writing a test program to see:

First, a string is stored NSString

   //Nsuserdefaults UseNsuserdefaults *defaults=[Nsuserdefaults Standarduserdefaults]; NSString*nameforsave =@"WHOISLCJ"; NSString*key=@"name"; //Save Data[Defaults setobject:nameforsave Forkey:key];//save name based on key value pair//reading DataNSString *nameforget = [defaults objectforkey:key];//remove name based on key valueNSLog (@"nameforget---%@", Nameforget); NSLog (@"nameforsave==nameforget---%i", [Nameforget Isequaltostring:nameforsave]);

Then store a Nsarray

    Nsarray *myarray = [Nsarray arraywithobjects:@ "who"@ "islcj" , nil];    [Defaults setobject:myarray forkey: @" Testarray " ];      *myarray1 = [Defaults arrayforkey:@ "testarray"];     NSLog (@ "myArray---%@", myArray1);

is not very simple, the other basic types are not implemented. This is just thinking, since it's setobject. Support for custom object types is not supported, so a custom object is written

    Person *person1=[[Person alloc]init];    Person1.name=@ "whoislcj";    [Defaults setobject:person1 forkey: @" Person1 " ];     *person2=[defaults Objectforkey:@ "person1"];    NSLog (@ "person2---%@", Person2.name);

Person.h

#import <Foundation/Foundation.h>@interface person:nsobject<nscoding>* name; @end

Person.m

#import "Person.h"@implementation Person#pragmaMark-nscoding delegate-(void) Encodewithcoder: (Nscoder *) Acoder {[Acoder encodeObject:self.name forkey:@"name"];}- (ID) Initwithcoder: (Nscoder *) Adecoder {self.name= [Adecoder Decodeobjectforkey:@"name"]; returnSelf ;}@end

Running result: Unfortunate thing happened, directly hung off, and then checked the information Nsuserdefaults itself does not support the storage of custom objects! There are many ways to do that,

The first custom object is converted into a JSON string store, and the second custom object is converted into NSData data.

used sharedpreferences all know the commit and apply function, all think of iOS this side will also have such considerations, so look at the original is the same, just call [defaults

Setobject:forkey:] After the nsuserdefaults is timed to write the data in the cache to disk, rather than instant write, in order to prevent the program exit after the completion of NSUSERDEFAULTS data loss caused by the You can use synchronize to force the data to be written to disk immediately after the data is written, with the following code:

// It is recommended to store the synchronization to disk, but it  is not necessary [Defaults synchronize];  

Then think of the sharedpreferences before the data cleared, then nsuserdefaults how to clear the data?

[[Nsuserdefaults Standarduserdefaults] removeobjectforkey:key]; [[Nsuserdefaults standarduserdefaults] synchronize];

Nsuserdefaults of iOS data storage

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.