1 storing data using plist files
The first thing to know is that using plist to store data, you can only store the data dictionary and array of OC, can not store the custom data model, example look at the style of the info.plist know
Storing data in a plist file
Gets the local sandbox path
nsarray *path = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);
Gets the full path
nsstring *docpath = [path objectatindex:0];
3. New data
Nsmutablearray *data = [[Nsmutablearray alloc] initwithobjects:@ "232", @ "Jee", nil];
NSString *filepath = [Docpath stringbyappendingpathcomponent:@ "Data.plist"];
NSString *str = @ "21323";
[Data insertobject:str atindex:0];
[Data Writetofile:filepath Atomically:yes];
Read and modify plist files
Gets the local sandbox path
nsarray *path = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);
Gets the full path
nsstring *docpath = [path objectatindex:0];
3. File path
nsstring *filepath = [Docpath stringbyappendingpathcomponent:@ "Data.plist"];
4. Read Data
nsmutablearray *data = [Nsmutablearray arraywithcontentsoffile:filepath];
5. Modify the data
nsstring *str = @ "wwwww";
[Data insertobject:str atindex:0];
[Data Writetofile:filepath Atomically:yes];
NSLog (@ "%@", data);
2. storing Data Using Archives
With archived storage, you can store custom model data.
First let model follow the Nscoding protocol
#import <Foundation/Foundation.h>
@interface subdata:nsobject<nscoding>
@property (copy, nonatomic) NSString * name;
@property (copy, nonatomic) NSString * code;
@property (copy, nonatomic) NSString * IMGURL;
@property (copy, nonatomic) NSString * SUB;
@property (copy, nonatomic) nsstring * URL;
@end
And then in the M file,
This method is invoked when a custom object is saved to a file///
in the method that describes how to store the properties of the custom object
//Also say in the method it is clear which properties of the custom object are stored
-(void) Encodewithcoder :(Nscoder *) acoder
{
[acoder encodeObject:self.name forkey:@ ' name '];
[Acoder encodeObject:self.code forkey:@ "code"];
[Acoder encodeObject:self.imgUrl forkey:@ "Imgurl"];
[Acoder encodeObject:self.sub forkey:@ "Sub"];
[Acoder encodeObject:self.url forkey:@ "url"];
}
This method is invoked when an object is read from a file///
In this method that shows how to read an object saved in a file
//That is to say in the method how to read the object in the file
-(ID) Initwithcoder: ( Nscoder *) Adecoder
{
//Note: The method in which the parent class needs to be initialized first
(Self=[super init]) {
Self.name=[adecoder decodeobjectforkey:@ "name"];
Self.code=[adecoder decodeobjectforkey:@ "Code"];
Self.imgurl=[adecoder decodeobjectforkey:@ "Imgurl"];
Self.sub=[adecoder decodeobjectforkey:@ "Sub"];
Self.url=[adecoder decodeobjectforkey:@ "url"];
return self;
}
So the setup in model is complete, followed by the code in use
Storing data in a file
Get file path
nsstring *docpath=[nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES) Lastobject];
NSString *path=[docpath stringbyappendingpathcomponent:@ "Recentlyused.archiver"];
Nsmutablearray *subdataarray = [[Nsmutablearray alloc] init];
[Subdataarray Insertobject:model atindex:0];
Saves a custom object to a file, calling the Nskeyedarchiver factory method Archiverootobject:tofile: Method
[Nskeyedarchiver archiverootobject: Subdataarray Tofile:path];
Solution file
1. Get file path
nsstring *docpath=[nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES) Lastobject];
NSString *path=[docpath stringbyappendingpathcomponent:@ "Recentlyused.archiver"];
2. Read the object from the file, the Solution object will call a Nskeyedunarchiver factory method Unarchiveobjectwithfile: You can.
if ([Gygeneraltools isfileexist:@ "Recentlyused.archiver"]) {
Self.recentlyusedarray = [Nskeyedunarchiver Unarchiveobjectwithfile:path];
}