Explain the archiving of apps in IOS development and how preferences are stored _ios

Source: Internet
Author: User
Tags reserved

IOS Application Data storage mode (archive)

One, simple explanation

In the use of plist data storage and reading, only applicable to the system with some of the common types to use, and must first obtain the path relative trouble;
Preferences (keep everything under the same folder, and primarily to store settings for the application)
Archiving: Because the first two have a fatal flaw, you can only store commonly used types. Archiving enables you to store your custom objects in a file.
Second, the code example

1. Document structure

2. code example

YYVIEWCONTROLLER.M file

Copy Code code as follows:

//
Yyviewcontroller.m
02-Archive
//
Created by Apple on 14-6-7.
Copyright (c) 2014 itcase. All rights reserved.
//

#import "YYViewController.h"
#import "YYPerson.h"

@interface Yyviewcontroller ()
-(Ibaction) Savebtnonclick: (ID) sender;
-(Ibaction) Readbtnonclick: (ID) sender;

@end


Copy Code code as follows:

@implementation Yyviewcontroller

-(void) viewdidload
{
[Super Viewdidload];
}


-(ibaction) Savebtnonclick: (ID) Sender {
   //1. Create Object
    Yyperson *p=[[ Yyperson Alloc]init];
    p.name=@ "Top of the text";
    p.age=23;
    p.height=1.7;
   
   //2. Get file path
    nsstring *docpath=[ Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) lastobject];
    nsstring *path=[docpath stringbyappendingpathcomponent:@ "Person.yangyang"];
    NSLog (@ "path=%@", Path);
   
   //3. Save a custom object to a file
    [nskeyedarchiver Archiverootobject:p Tofile:path];
   
}

-(Ibaction) Readbtnonclick: (ID) Sender {
1. Get File path
NSString *docpath=[nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) lastObject];
NSString *path=[docpath stringbyappendingpathcomponent:@ "Person.yangyang"];
NSLog (@ "path=%@", Path);

2. Reading objects from files
Yyperson *p=[nskeyedunarchiver Unarchiveobjectwithfile:path];
NSLog (@ "%@,%d,%.1f", p.name,p.age,p.height);
}
@end


Create a new Person class

YYPerson.h file

Copy Code code as follows:

//
YYPerson.h
02-Archive
//
Created by Apple on 14-6-7.
Copyright (c) 2014 itcase. All rights reserved.
//

#import <Foundation/Foundation.h>

If you want to save a custom object to a file, you must implement the Nscoding protocol
@interface yyperson:nsobject<nscoding>

Name
@property (nonatomic,copy) NSString *name;
Age
@property (nonatomic,assign) int age;
Height
@property (nonatomic,assign) double height;
@end


YYPERSON.M file
Copy Code code as follows:

//
Yyperson.m
02-Archive
//
Created by Apple on 14-6-7.
Copyright (c) 2014 itcase. All rights reserved.
//

#import "YYPerson.h"

@implementation Yyperson

This method is called when a custom object is saved to a file
Describes how to store properties of custom objects in this method
It says in this method that it is clear which properties of the stored custom object
-(void) Encodewithcoder: (Nscoder *) Acoder
{
NSLog (@ "called Encodewithcoder: Method");
[Acoder encodeObject:self.name forkey:@ "name"];
[Acoder encodeInteger:self.age forkey:@ "age"];
[Acoder encodeDouble:self.height forkey:@ "height"];
}

This method is invoked when an object is read from a file
Describes how to read objects saved in a file in this method
That is, in this method, it's clear how to read objects in a file.
-(ID) Initwithcoder: (Nscoder *) Adecoder
{
NSLog (@ "called Initwithcoder: Method");
Note: Methods in the construction method that need to initialize the parent class first
if (Self=[super init]) {
Self.name=[adecoder decodeobjectforkey:@ "name"];
Self.age=[adecoder decodeintegerforkey:@ "age"];
Self.height=[adecoder decodedoubleforkey:@ "height"];
}
return self;
}
@end


3. Printing effect and two important error tips

Click the Save button and the Read button to successfully print the results as follows:

About error tips for not implementing two protocol methods:
-(void) Encodewithcoder: (Nscoder *) Acoder method:

-(ID) Initwithcoder: (Nscoder *) Adecoder method:

Iii. use in the Inheritance class

Create a new student class, and let this class inherit from the Preson class, adding a weight to the property.

YYstudent.h file

Copy Code code as follows:

//
YYstudent.h
02-Archive
//
Created by Apple on 14-6-7.
Copyright (c) 2014 itcase. All rights reserved.
//

#import "YYPerson.h"

@interface Yystudent:yyperson
Add a weight attribute
@property (nonatomic,assign) double weight;
@end


YYSTUDENT.M file
Copy Code code as follows:

//
Yystudent.m
02-Archive
//
Created by Apple on 14-6-7.
Copyright (c) 2014 itcase. All rights reserved.
//

#import "YYstudent.h"

@implementation Yystudent

Override these two methods in a subclass
-(void) Encodewithcoder: (Nscoder *) Acoder
{
[Super Encodewithcoder:acoder];
NSLog (@ "called the Yystudent Encodewithcoder");
[Acoder encodeFloat:self.weight forkey:@ "weight"];
}

-(ID) Initwithcoder: (Nscoder *) Adecoder
{
if (self = [Super Initwithcoder:adecoder]) {
NSLog (@ "called the Yystudent Initwithcoder");
Self.weight = [Adecoder decodefloatforkey:@ "weight"];
}
return self;
}
@end


YYVIEWCONTROLLER.M file
Copy Code code as follows:

//
Yyviewcontroller.m
02-Archive
//
Created by Apple on 14-6-7.
Copyright (c) 2014 itcase. All rights reserved.
//

#import "YYViewController.h"
#import "YYPerson.h"
#import "YYstudent.h"

@interface Yyviewcontroller ()
-(Ibaction) Savebtnonclick: (ID) sender;
-(Ibaction) Readbtnonclick: (ID) sender;

@end


Copy Code code as follows:

@implementation Yyviewcontroller

-(void) viewdidload
{
[Super Viewdidload];
}


-(Ibaction) Savebtnonclick: (ID) Sender {
1. Creating objects
Yyperson *p=[[yyperson Alloc]init];
p.name=@ "Wen ding Ding";
p.age=23;
p.height=1.7;

Yystudent *s=[[yystudent Alloc]init];
s.name=@ "Wendingding";
s.age=23;
s.height=1.7;
s.weight=62;
2. Get File path
NSString *docpath=[nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) lastObject];
NSString *path=[docpath stringbyappendingpathcomponent:@ "Person.yangyang"];
NSLog (@ "path=%@", Path);

3. Save a custom object to a file
[Nskeyedarchiver archiverootobject:p Tofile:path];
[Nskeyedarchiver archiverootobject:s Tofile:path];

}

-(Ibaction) Readbtnonclick: (ID) Sender {
1. Get File path
NSString *docpath=[nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) lastObject];
NSString *path=[docpath stringbyappendingpathcomponent:@ "Person.yangyang"];
NSLog (@ "path=%@", Path);

2. Reading objects from files
Yyperson *p=[nskeyedunarchiver Unarchiveobjectwithfile:path];
NSLog (@ "%@,%d,%.1f", p.name,p.age,p.height);
Yystudent *s=[nskeyedunarchiver Unarchiveobjectwithfile:path];
NSLog (@ "%@,%d,%.1f,%f", s.name,s.age,s.height,s.weight);
}
@end


Iv. Important Notes

1. Save Data process:

Copy Code code as follows:

1. Creating objects
Yystudent *s=[[yystudent Alloc]init];
s.name=@ "Wendingding";
s.age=23;
s.height=1.7;
s.weight=62;

2. Get File path
NSString *docpath=[nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) lastObject];
NSString *path=[docpath stringbyappendingpathcomponent:@ "Person.yangyang"];
NSLog (@ "path=%@", Path);

3. Save a custom object to a file
[Nskeyedarchiver archiverootobject:s Tofile:path];

1. Creating objects
Yystudent *s=[[yystudent Alloc]init];
s.name=@ "Wendingding";
s.age=23;
s.height=1.7;
s.weight=62;

2. Get File path
NSString *docpath=[nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) lastObject];
NSString *path=[docpath stringbyappendingpathcomponent:@ "Person.yangyang"];
NSLog (@ "path=%@", Path);

3. Save a custom object to a file
[Nskeyedarchiver archiverootobject:s Tofile:path];


How iOS applies data storage (preference settings)
First, a brief introduction

Many iOS apps support preferences, such as saving user names, passwords, font sizes, and so on, and iOS provides a standard solution for adding preferences to applications

Each application has a nsuserdefaults instance, which is used to access preference settings. For example, save the user name, font size, or whether to log on automatically

Storage location:

Storage form:

Second, the code example

1.storyboard

2. Code

Copy Code code as follows:

//
Yyviewcontroller.m
01-Preference Settings
//
Created by Apple on 14-6-7.
Copyright (c) 2014 itcase. All rights reserved.
//

#import "YYViewController.h"
Preference settings
@interface Yyviewcontroller ()
/**
* Save Data
*/
-(Ibaction) SaveData: (ID) sender;
/**
* Read Data
*/
-(Ibaction) ReadData: (ID) sender;

@end


Copy Code code as follows:

@implementation Yyviewcontroller

-(Ibaction) SaveData: (ID) Sender {
1. Get Nsuserdefaults Object
Nsuserdefaults *defaults=[nsuserdefaults Standarduserdefaults];

2 Save data (if there is no synchronization after setting the data, the data will be automatically saved to the Preferences folder at some point in the future)
[Defaults setobject:@ "Yangyong" forkey:@ "name"];
[Defaults setinteger:23 forkey:@ "age"];
[Defaults setdouble:1.73f forkey:@ "height"];
[Defaults setobject:@ "Man" forkey:@ "gender"];

3. Force the data to be saved immediately
[Defaults synchronize];
}

-(Ibaction) ReadData: (ID) Sender {
1. Get Nsuserdefaults Object
Nsuserdefaults *defaults=[nsuserdefaults Standarduserdefaults];
Read the saved data
NSString *name=[defaults objectforkey:@ "name"];
NSString *gender=[defaults objectforkey:@ "gender"];
Nsinteger age=[defaults integerforkey:@ "age"];
Double height=[defaults doubleforkey:@ "height"];
Print data
NSLog (@ "name=%@,gender=%@,age=%d,height=%.1f", name,gender,age,height);
}
@end


3. Click Save data, read the data button to print the following

Iii. Supplementary Notes

1. Save data

Copy Code code as follows:

1. Get Nsuserdefaults Object
Nsuserdefaults *defaults=[nsuserdefaults Standarduserdefaults];

2 Saving data
[Defaults setobject:@ "Yangyong" forkey:@ "name"];
[Defaults setinteger:23 forkey:@ "age"];
[Defaults setdouble:1.73f forkey:@ "height"];
[Defaults setobject:@ "Man" forkey:@ "gender"];

3. Force the data to be saved immediately
[Defaults synchronize];

2. Reading data
Copy Code code as follows:

1. Get Nsuserdefaults Object
Nsuserdefaults *defaults=[nsuserdefaults Standarduserdefaults];
2. Read the Saved data
NSString *name=[defaults objectforkey:@ "name"];
NSString *gender=[defaults objectforkey:@ "gender"];
Nsinteger age=[defaults integerforkey:@ "age"];
Double height=[defaults doubleforkey:@ "height"];

3. Important note

(1) Preferences are designed to hold application configuration information, and generally do not save other data in preferences. If you use System Preferences to store data, the default is to store it under the Preferences folder, and the preferences will save all the data to the same file.

(2) After the data is saved by using preferences, the time it is saved to the system is indeterminate, and will automatically save the data to the Preferences folder at some point in the future, if you need to store the data immediately, you can use [defaults synchronize];

(3) Note: All the information is written in a file, compared to the simple plist can save and read the basic data types.

(4) Step: Get Nsuserdefaults, Save (read) data

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.