Home>

xml attribute list-plist

Application SandboxEach ios application has a "should" sandbox (application sandbox is the file system directory), which is isolated from other file systems.Should⽤must stay in his own sandbox,Other applications cannot access the sandbox (hint:access is already open in ios8)

The application's file system in the sandbox should be recorded as shown below (assuming the application name is layer)

The simulator should be rooted in the sandbox at:(apple is the username, 7.0 is the simulator version)/users/apple/library/application support/iphone simulator/7.0/applications

Application Sandbox Structure Analysis

Should⽤package:(layer in the picture above) contains all the resource files and executable files

documents:holds data that should be persisted generated by the runtime,Itunes backs up this directory when it syncs the device.For example, the game app can save the game archive in this directory

tmp:saves the temporary data needed by the runtime,After "#12132" is finished, delete the corresponding file from this directory.When the application is not running,The system may also clear files in this directory.iTunes does not back up the directory when syncing the device

library/caches:saves the application runtime's persistent data,iTunes does not back up the directory when synchronizing the device.⼀Non-critical data with large storage volume and no backup required

library/preference:save all app preferences,The settings of ios should look for the settings of the settings in this record.iTunes backs up the directory when syncing the device

Third, the common acquisition method of application sandbox

Sandbox root directory:nsstring * home=nshomedirectory ();

documents:(2 ways)

Stitching "documents" strings using the sandbox root

nsstring * home=nshomedirectory ();
nsstring * documents=[home stringbyappendingpathcomponent:@"documents"];//Not recommended,Because the new version of the operating system may modify the directory name

Use nssearchpathfordirectoriesindomains function

//nsuserdomainmask means look under the user folder
//yes represents the wavy character "~" in the expanded path
nsarray * array=nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, no);//In iOS, only one directory matches the parameters passed in
So there is only one element in this set
</p>
<p>
nsstring * documents=[array objectatindex:0];
</p>
<p>
tmp:nsstring * tmp=nstemporarydirectory ();

library/caches:(2 methods⽅similar to documents)

Concatenate "caches" strings using the sandbox root

Lee⽤nssearchpathfordirectoriesindomains function

library/preference:access the setting information in this directory through the nsuserdefaults class

The corresponding code:

#import "njviewcontroller.h"
#import "njperson.h"
</p>
<p>
@interface njviewcontroller ()
-(ibaction) savedatabtnclick:(id) sender;
-(ibaction) readdatabtnclick:(id) sender;
</p>
<p>
@end

@implementation njviewcontroller
/**
* Click the save button
* /
-(ibaction) savedatabtnclick:(id) sender {
 //youtube practices
//nsstring * path [email protected]"/users/apple/library/application support/iphone simulator/7.1/applications/a6d53e11-ddf0-4392-b2d4-fe77a96888a6/documents/abc.plist";
 //Get the application root directory
 nsstring * home=nshomedirectory ();
 //not recommended to write /
 //nsstring * path=[home stringbyappendingstring:@"/documents"];
 //do not recommend writing documents
 //nsstring * path=[home stringbyappendingpathcomponent:@"documents"];
 //nsuserdomainmask looks in the user directory
 //yes represents the user directory ~
 //nsdocumentdirectory find documents folder
 //It is recommended to use the following method to obtain dynamically
 nsstring * doc=[nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes) lastobject];
 //stitching file path
 nsstring * path=[doc stringbyappendingpathcomponent:@"abc.plist"];
 nslog (@ "%@", path);
 //nsarray * arr [email protected][@ "lnj",@"28"];
 //[arr writetofile:path atomically:yes];
 //nsdictionary * dict [email protected]{@ "name":@"lnj",@"age":@"28"};
 //call writetofile to write data to a file
 //[dict writetofile:path atomically:yes];
 /*
 plist can only store some regular classes that come with the system, That is, an object with a writetofile method can use plist to save data.
 String/dictionary/data/nsnumber/nsdata ...
 * /
 //custom objects cannot be saved to plist
 njperson * p=[[njperson alloc] init];
 p.name [email protected]"lnj";
 nsdictionary * dict [email protected]{@ "person":@"abc"};
 [dict writetofile:path atomically:yes];
}
/**
* Click the read button
* /
-(ibaction) readdatabtnclick:(id) sender {
 nsstring * doc=[nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes) lastobject];
 nsstring * path=[doc stringbyappendingpathcomponent:@"abc.plist"]
 ;
 //read data
 nsdictionary * dict=[nsdictionary dictionarywithcontentsoffile:path];
 nslog (@ "%@", dict);
}
@end

Fourth, the property list

The attribute list is a file in xml format,Extension plist

If the object is of type nsstring, nsdictionary, nsarray, nsdata, nsnumber, etc., you can use the writetofile:atomically:⽅method to write the object directly to the attribute list file

nskeydearchiver archive

First, a brief description

When using plist for data storage and reading,Only applicable to some common types that come with the system,And it must be relatively troublesome to get the path first;

Preferences (save everything in the same folder,And is mainly used to store application settings)

Archive:Because the first two have a fatal flaw,Only common types can be stored.Archiving allows you to store custom objects in files.

Second, the code example

File structure

//
//yyviewcontroller.m
//02-archive
//
//created by apple on 14-6-7.
//copyright (c) 2014 itcase. all rights reserved.
//
</p>
<p>
#import "yyviewcontroller.h"
#import "yyperson.h"
</p>
<p>
@interface yyviewcontroller ()
-(ibaction) savebtnonclick:(id) sender;
-(ibaction) readbtnonclick:(id) sender;
</p>
<p>
@end

@implementation yyviewcontroller
</p>
<p>
-(void) viewdidload
{
 [super viewdidload];
}
</p>
<p>
-(ibaction) savebtnonclick:(id) sender {
 //1. Create object
 yyperson * p=[[yyperson alloc] init];
 p.name [email protected]"";
 p.age=23;
 p.height=1.7;
 //2. Get the file path
 nsstring * docpath=[nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes) lastobject];
 nsstring * path=[docpath stringbyappendingpathcomponent:@"person.yangyang"];
 nslog (@ "path =%@", path);
 //3. Save the custom object to a file
 [nskeyedarchiver archiverootobject:p tofile:path];
}
</p>
<p>
-(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. Read object from file
 yyperson * p=[nskeyedunarchiver unarchiveobjectwithfile:path];
 nslog (@ "%@,%d,%. 1f", p.name, p.age, p.height);
}
@end

Create a new person class

yyperson.h file

//
//yyperson.h
//02-archive
//
//created by apple on 14-6-7.
//copyright (c) 2014 itcase. all rights reserved.
//
</p>
<p>
#import<foundation/foundation.h>
</p>
<p>
//If i want to save a custom object to a file, you must implement the nscoding protocol
@interface yyperson:nsobject<nscoding>
</p>
<p>
//Name
@property (nonatomic, copy) nsstring * name;
//age
@property (nonatomic, assign) int age;
//height
@property (nonatomic, assign) double height;
@end

yyperson.m file

//
//yyperson.m
//02-archive
//
//created by apple on 14-6-7.
//copyright (c) 2014 itcase. all rights reserved.
//
</p>
<p>
#import "yyperson.h"
</p>
<p>
@implementation yyperson
</p>
<p>
//This method is called when a custom object is saved to a file
//In this method, explain how to store the properties of the custom object
//That is, in this method, it is clear which attributes of the custom object are stored
-(void) encodewithcoder:(nscoder *) acoder
{
 nslog (@ "The encodewithcoder:method was called");
 [acoder encodeobject:self.name forkey:@"name"];
 [acoder encodeinteger:self.age forkey:@"age"];
 [acoder encodedouble:self.height forkey:@"height"];
}
</p>
<p>
//This method is called when an object is read from a file
//In this method, explain how to read the object saved in the file
//That is to say in this method how to read the objects in the file
-(id) initwithcoder:(nscoder *) adecoder
{
 nslog (@ "Initwithcoder:method called");
 //Note:the method of the parent class needs to be initialized first in the constructor
 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,The successful print results are as follows:

Error message about not implementing two protocol methods:

-(void) encodewithcoder:(nscoder *) acoder method:

-(id) initwithcoder:(nscoder *) adecoder method:

Use in inherited classes

Create a new student class,Let this class inherit from the preson class and add a weight attribute.

yystudent.h file

//
//yystudent.h
//02-archive
//
//created by apple on 14-6-7.
//copyright (c) 2014 itcase. all rights reserved.
//
</p>
<p>
#import "yyperson.h"
</p>
<p>
@interface yystudent:yyperson
//Add a weight attribute
@property (nonatomic, assign) double weight;
@end

yystudent.m file

//
//yystudent.m
//02-archive
//
//created by apple on 14-6-7.
//copyright (c) 2014 itcase. all rights reserved.
//
</p>
<p>
#import "yystudent.h"
</p>
<p>
@implementation yystudent
</p>
<p>
//Override these two methods in the subclass
-(void) encodewithcoder:(nscoder *) acoder
{
 [super encodewithcoder:acoder];
 nslog (@ "Called yystudent encodewithcoder");
 [acoder encodefloat:self.weight forkey:@"weight"];
}
</p>
<p>
-(id) initwithcoder:(nscoder *) adecoder
{
 if (self=[super initwithcoder:adecoder]) {
 nslog (@ "Called yystudent initwithcoder");
 self.weight=[adecoder decodefloatforkey:@"weight"];
 }
 return self;
}
@end

yyviewcontroller.m file

//
//yyviewcontroller.m
//02-archive
//
//created by apple on 14-6-7.
//copyright (c) 2014 itcase. all rights reserved.
//
</p>
<p>
#import "yyviewcontroller.h"
#import "yyperson.h"
#import "yystudent.h"
</p>
<p>
@interface yyviewcontroller ()
-(ibaction) savebtnonclick:(id) sender;
-(ibaction) readbtnonclick:(id) sender;
</p>
<p>
@end

@implementation yyviewcontroller
</p>
<p>
-(void) viewdidload
{
 [super viewdidload];
}
</p>
<p>
-(ibaction) savebtnonclick:(id) sender {
 //1. Create object
//yyperson * p=[[yyperson alloc] init];
//p.name [email protected]"";
//p.age=23;
//p.height=1.7;
 yystudent * s=[[yystudent alloc] init];
 s.name [email protected]"wendingding";
 s.age=23;
 s.height=1.7;
 s.weight=62;
 //2. Get the file path
 nsstring * docpath=[nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes) lastobject];
 nsstring * path=[docpath stringbyappendingpathcomponent:@"person.yangyang"];
 nslog (@ "path =%@", path);
 //3. Save the custom object to a file
//[nskeyedarchiver archiverootobject:p tofile:path];
 [nskeyedarchiver archiverootobject:s tofile:path];
}
</p>
<p>
-(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. Read object from file
//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

Printout after clicking save button and read button:

Important Notes

1. The process of saving data:

//1. Create object
 yystudent * s=[[yystudent alloc] init];
 s.name [email protected]"wendingding";
 s.age=23;
 s.height=1.7;
 s.weight=62;
 //2. Get the file path
 nsstring * docpath=[nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes) lastobject];
 nsstring * path=[docpath stringbyappendingpathcomponent:@"person.yangyang"];
 nslog (@ "path =%@", path);
 //3. Save the custom object to a file
 [nskeyedarchiver archiverootobject:s tofile:path];

2. Process of reading data:

//1. Get file path
 nsstring * docpath=[nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes) lastobject];
 nsstring * path=[docpath stringbyappendingpathcomponent:@"person.yangyang"];
 //2. Read object from file
 yystudent * s=[nskeyedunarchiver unarchiveobjectwithfile:path];

3. Observe the nscoding protocol and implement the two methods in the protocol.

4. If it is inheritance,Then the subclass must override those two methods.Because when the subclass of person accesses,Will go to the subclass to find the called method,If it doesn't find it then go to the parent class,Therefore, the newly added attributes will be ignored during the last save and read.Need to call the parent method first,First initialize the parent class,Reinitialize the subclasses.

5. The suffix name of the file where the data is saved can be named as you like.

6. The data saved through plist is displayed directly,Not safe. The data saved by the archive method is garbled when opened in the file,safer.

ios
  • Previous Method to solve the problem that SpringMVC cannot receive parameter values ​​after angular post request
  • Next jQuery click to change class and toggle and toggleClass () method definition usage