List of---Properties for iOS data persistence

Source: Internet
Author: User
Tags list of attributes

Attribute list (plist)

iOS provides a plist format file (a list of attributes) for storing lightweight data, and only Nsdictionary, Nsarray, NSString, NSNumber, Boolean, NSData, and nsdate types of data can be saved. Save these types of data in a plist format file that can be read directly using Nsdictionary and Nsarray

(i), the use of nsuserdefault to achieve persistence

The following is a list of nsuserdefault locally saved locations, which are mentioned in the Data Persistence sandbox directory. Library/preferences the plist file in this directory is its saved directory.
The use of Nsuserdefault is mainly to save and read

Initialize a Nsuserdefault

    + (NSUserDefaults *)standardUserDefaults;

Ways to set up your data

- (void) SetObject: (NullableID) value Forkey: (NSString *) DefaultName; - (void) Setinteger: (nsinteger) value Forkey: (nsstring *) DefaultName; -(void) SetFloat: (float) value Forkey: (   nsstring *) DefaultName; -(void) setdouble: (double) value Forkey: (nsstring *) DefaultName; -(void) Setbool: (bool) value Forkey: (   nsstring *) DefaultName; -(void) SetUrl: (Nullable nsurl *) URL Forkey: (nsstring *) defaultname ns_available (10_6, Span class= "Hljs-number" >4_0)              

How to read data:

- (NullableIdObjectforkey:(NSString *) DefaultName;- (NullableNSString *)Stringforkey:(NSString *) DefaultName;- (NullableNsarray *)Arrayforkey:(NSString *) DefaultName;- (Nullablensdictionary<NSString *,Id> *)Dictionaryforkey:(NSString *) DefaultName;- (NullableNSData *)Dataforkey:(NSString *) DefaultName;- (Nullablensarray<NSString *> *)Stringarrayforkey:(NSString *) DefaultName;- (Nsinteger)Integerforkey:(NSString *) DefaultName;-(float)    Floatforkey:-(double)    Doubleforkey:-(bool)    Boolforkey:-(nullable nsurl *)  urlforkey: (NSString *) defaultname ns_available (10_6, 4_0);    

Ways to delete data:

    - (void)removeObjectForKey:(NSString *)defaultName;

Save data:

     // 如果不手动调用,系统会自动保存,但时间不定

   - (BOOL)synchronize;
 

   // 存储id类型数据

+ (void) SetValue: (ID) value Andkey: (NSString *) key

{

Nsuserdefaults *userdefaults = [Nsuserdefaults standarduserdefaults];

[Userdefaults Setobject:value Forkey:key];

[Userdefaults Synchronize];

}

    // 获取数据

+ (NSString *) Getvaluebykey: (NSString *) key

{

Nsuserdefaults * settings = [Nsuserdefaults standarduserdefaults];

NSString *value = [Settings Objectforkey:key];

return value;

}

 

(ii), manually add the plist file

  New File-->resource-->property List

    The root type of the plist file can only be Nsarray or nsdictionary

    

read the data in the plist file into the corresponding root type

    1. Get the path where the file is located, Resource: file name, Type: file format

    NSString *filepath = [[nsbundle Mainbundle] Pathforresource:@ "Userinfos" OfType:@ "plist"];

    2. Get the data in the corresponding format from the path

    If Root is Nsarray, the array is used to save

    Nsarray *infos = [nsarray arraywithcontentsoffile:filepath];

    NSLog (@ "%@", infos);

< Span class= "hljs-string" > //if Root is nsdictionary, use dictionary to save

< Span class= "hljs-string" > nsdictionary *infodic = [ Nsdictionary Dictionarywithcontentsoffile:filepath];

    NSLog (@ "%@", infodic);

(iii), direct data written to the plist file

Because Nsuserdefault is essentially the persistence of attributes through the plist file. Therefore, we can create a plist file by ourselves to achieve the persistence of the property.

          Nsarray *path =Nssearchpathfordirectoriesindomains (NSDocumentDirectory,Nsuserdomainmask,YES); nsstring *docpath = [path Objectatindex:0]; nsstring *myfile = [DocPath stringbyappendingpathcomponent:@ "Test.plist"]; nsmutabledictionary *contentdic; //Determine if there is a local plist fileif ([[Nsfilemanager Defaultmanager] fileexistsatpath:myfile] = =NO) {nsfilemanager* fm = [nsfilemanager Defaultmanager];  //create a file [FM createfileatpath:myfile contents:
                      nil attributes:     nil ";    Contentdic = [[ nsmutabledictionary alloc] init]; } else {contentdic = [[nsmutabledictionary Alloc] InitWithCon    Tentsoffile:myfile]; } //data read and write operations [Contentdic setobject:@ "1234" Forkey:@ "PassWord"]; //save modified data to plist file [contentdic writetofile:myfile Atomically:yes";             

(iv), summary
    The plist file reads and writes more efficiently, because his read and write needs to take all the data out and then save it all, so it is only suitable for small data.

List of---Properties for iOS data persistence

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.