Data serialization, persistence, archiving
1.Foundation
2. Data attribute list (nsarray,nsdictionary)
3.sqlite,cocodata
Archived file data is automatically encrypted, no longer plaintext, and is limited by the fact that only one object can be archived
/** Archiving, data persistence, data serialization **/
/*
NSString *filepath = [Nshomedirectory () stringbyappendingpathcomponent:@ "Documents/text.arc"];
Nsarray *array = @[@ "Jack", @ "Tom", @ "DAV", @123789];
BOOL success = [Nskeyedarchiver Archiverootobject:array Tofile:filepath];
if (success) {
NSLog (@ "File archive success!!! ");
}
*/
/** Archive ***/
NSString *filepath = [Nshomedirectory () stringbyappendingpathcomponent:@ "Documents/text.arc"];
Nsarray *array = [Nskeyedunarchiver Unarchiveobjectwithfile:filepath];
NSLog (@ "array:%@", array);
To archive multiple objects, customize the way
----------------1. The second type of archiving --------------------------------
/ * Custom archive **/
/*
NSString *filepath = [Nshomedirectory () stringbyappendingpathcomponent:@ "Documents/coustom.archiver"];
Nsmutabledata *data = [Nsmutabledata data];
Nskeyedarchiver *archiverfile = [[Nskeyedarchiver alloc] initforwritingwithmutabledata:data];
Nsarray *array = @[@ "Jack", @ "Tom"];
[Archiverfile encodeint:100 forkey:@ "age"];
[Archiverfile encodeobject:array forkey:@ "name"];
[Archiverfile finishencoding];
[Data Writetofile:filepath Atomically:yes];
[Archiverfile release];
*/
/** Custom Solution archive ***/
nsstring *filepath = [nshomedirectory() stringbyappendingpathcomponent:@ "Documents /coustom.archiver "];
nsdata *data = [nsdata datawithcontentsoffile: FilePath];
nskeyedunarchiver *unarchiver = [[nskeyedunarchiver alloc] initforreadingwithdata: Data];
int Age = [Unarchiver decodeintforkey:@ ' age '];
nsarray *arrayname = [Unarchiver decodeobjectforkey:@ "name"];
[Unarchiver release];
NSLog (@ "age=%d,arrayname:%@", age,arrayname);
The object to implement the archive is to implement the agent <NSCoding>
Archive
-(void) Encodewithcoder: (nscoder *) acoder
{
[Acoder encodeobject:_name forkey:name];
[Acoder encodeobject:_email forkey:email];
[Acoder encodeobject:_password forkey:password];
[Acoder encodeint:_age forkey:age];
}
solution file
-(ID) Initwithcoder: (nscoder *) adecoder
{
self = [super init];
if (self! = nil) {
_age = [Adecoder decodeintforkey:age];
_name = [[Adecoder decodeobjectforkey:name] copy];
self. email = [Adecoder decodeobjectforkey:email];
self. Password = [Adecoder decodeobjectforkey:password];
}
return self ;
}
-(nsstring *) Description
{
nsstring *str = [nsstring stringwithformat:@ "name==%@,age==%d,email==%@,password==%@" ,_name,_age,_email,_password];
return str;
}
Data serialization, persistence, archiving