The OS file localization processing takes three steps, obtains the file saves the path, chooses the corresponding archive method according to the file attribute, the archival reading document implementation.
First, get the file save path
1. "Application Package": This is where the application's source files, including resource files and executables, are stored.
NSString *path = [[NSBundle mainbundle] bundlepath];
2.Documents: The most commonly used directory, itunes synchronizes the content in this folder when syncing the app, and is ideal for storing important data.
NSString *path = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES). FirstObject;
3.library/caches:itunes does not synchronize this folder for non-critical data that is large in size and does not need to be backed up.
NSString *path = Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES). FirstObject;
4.tmp:itunes does not synchronize this folder, the system may delete the files under this directory when the app is not running, so this directory is suitable for saving some temporary files in the app and deleting them when they are exhausted.
NSString *path = Nstemporarydirectory ();
5.library/preferences:itunes syncing the app synchronizes the contents of this folder, usually saving the app's settings information
Nsuserdefaults *userdefaults = [Nsuserdefaults standarduserdefaults];
Two: Different file attributes need to have different file save processing mode
File formats typically saved in 1.plist files
Nsarray;
Nsmutablearray;
Nsdictionary;
Nsmutabledictionary;
NSData;
Nsmutabledata;
NSString;
nsmutablestring;
NSNumber;
NSDate;
(1) Get File save path
(2) Save
[Array writetofile:filename atomically:yes];
(3) Read
Nsarray *result = [Nsarray arraywithcontentsoffile:filename];
(4) Note
Only the types listed above can use the Plist file store.
Use writetofile:atomically when storing : method. Where atomically indicates whether a secondary file needs to be written before the secondary file is copied to the destination file address. This is a more secure way to write to a file, and generally write yes.
Use Arraywithcontentsoffile when reading : method.
2.Preference configuration file
(1) Get Nsuserdefaults file
Nsuserdefaults *userdefaults = [Nsuserdefaults standarduserdefaults];
(2) Writing content to a file
[Userdefaults setobject:@ "AAA" forkey:@ "a"];
Note: [userdefaults setobject: Value forkey: keywords]
(3) Sync to config file
[Userdefaults Synchronize];
(4) Read the file
NSString *name = [Userdefaults objectforkey:@ "a"]; Read the value of "a" in the configuration file
BOOL sex = [userdefaults boolforkey:@ "sex"];
Note: To use the corresponding object type to read the values in the configuration file, you need to use the corresponding method.
(5) Note
Preferences are designed to save configuration information for your application, and you should not save other data in your preferences.
If the Synchronize method is not called, the system will be saved to the file in an indefinite time, depending on the I/O situation. So if you need to write the file immediately, you must call the Synchronize method.
Preferences will save all the data in the same file. A plist file that is named under this application package name in the preference directory.
3.Nskeyedarchiver Archive
Archiving is another form of serialization in iOS, as long as objects that follow the Nscoding protocol can be serialized through it. Because most of the foundation and cocoa touch classes that support storing data adhere to the Nscoding protocol, archiving is relatively easy to implement for most classes.
(1) Comply with the Nscoding protocol in the object that needs to be archived, and use the corresponding method
Abide by the nscoding protocol in the. h file
@interface Person:nsobject
How to implement the Nscoding protocol in. m files
Archive
-(void) Encodewithcoder: (Nscoder *) Acoder {
[Acoder encodeObject:self.avatar forkey:@ "Avatar"];
[Acoder encodeobject:self.nameforkey:@ "name"];
[Acoder encodeInteger:self.age forkey:@ "age"];
}
Solution file
-(ID) Initwithcoder: (Nscoder *) Adecoder {
if ([Superinit]) {
Self.avatar = [Adecoder decodeobjectforkey:@ "Avatar"];
self.name= [Adecoder decodeobjectforkey:@ "name"];
Self.age = [Adecoder decodeintegerforkey:@ "age"];
}
returnself;
}
Note: To archive subclasses, you must implement [Super Encodeobject:acoder]; [Super Initwithcoder:adecoder]; method
(2) Use:
Archive using Archiverootobject:tofile in Nskeyedarchiver: Method
1. Create a file path
NSString *file = [Nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES). FirstObject stringbyappendingpathcomponent:@ "Person.data"];
2. Archive to file directory
[Nskeyedarchiver Archiverootobject:person Tofile:file];
Reading requires the use of Unarchiveobjectwithfile in Nskeyedunarchiver: Method
1. Create a file path
NSString *file = [Nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES). FirstObject stringbyappendingpathcomponent:@ "Person.data"];
2. read files in the file directory
Person *person = [Nskeyedunarchiver unarchiveobjectwithfile:file];
(3) Note
The nscoding protocol must be followed and implemented
The extension of the saved file can be specified arbitrarily
You must first call the parent class's archive solution method when inheriting
iOS Development file path save