Objective:
The day before yesterday to learn nsuserdefaults, we know nsuserdefaults can not save the custom object, so we come today to meet the archive and reconciliation file. Let's start by reviewing how Java is implemented to save a custom Object! First a custom object must implement the Serializable interface, and then serialize an object into binary data to write to a byte[] data or file, and vice versa to read binary data from a binary data or file into an object, So I think the archive file of iOS is just like the process of the serialization of Java, write a program to try it out.
Test the underlying type first:
//ArchiveNSString *path = [Nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES) ObjectAtIndex:0]; //String DataNSString *string=@"Who is LCJ?"; NSString*filename = [Path stringbyappendingpathcomponent:@"teststring"]; [Nskeyedarchiver archiverootobject:stringTofile:filename]; NSString*string1=[Nskeyedunarchiver Unarchiveobjectwithfile:filename]; NSLog (@"teststring---%@", string1); //Collection ArrayNsmutablearray *array = [Nsmutablearray arraywithobjects:@"W.H.O.",@"ISLCJ", nil]; FileName= [Path stringbyappendingpathcomponent:@"Testarray"]; [Nskeyedarchiver Archiverootobject:array Tofile:filename]; Nsarray*arr =[Nskeyedunarchiver Unarchiveobjectwithfile:filename]; NSLog (@"Testarray---%@", arr); //Dictionary Savefilename = [Path stringbyappendingpathcomponent:@"testnsdictionary"]; Nsdictionary*dictionary=[nsdictionary Dictionarywithobject:@"WHOISLCJ"Forkey:@"name"]; [Nskeyedarchiver archiverootobject:dictionary Tofile:filename]; Nsdictionary*dictionary1=[Nskeyedunarchiver Unarchiveobjectwithfile:filename]; NSLog (@"Dictionary---%@", Dictionary1);
It has been said that Java to achieve serialization deserialization must implement the Serializable interface, then iOS? iOS needs to implement the Nscoding protocol,
Person.h
#import <Foundation/Foundation.h>@interface person:nsobject<nscoding>* name; @end
Person.m
#import "Person.h"@implementation Person#pragmaMark-nscoding delegate-(void) Encodewithcoder: (Nscoder *) Acoder {[Acoder encodeObject:self.name forkey:@"name"];}- (ID) Initwithcoder: (Nscoder *) Adecoder {self.name= [Adecoder Decodeobjectforkey:@"name"]; returnSelf ;}@end
Specific implementation:
// custom Object Types Person *person=[[Person alloc]init]; Person.name=@ "whoislcj"; = [Path stringbyappendingpathcomponent:@ "testobject"]; [Nskeyedarchiver Archiverootobject:person tofile:filename]; *tempperson = [Nskeyedunarchiver unarchiveobjectwithfile:filename]; NSLog (@ "tempperson:%@", Tempperson.name);
Archive how to delete
// Delete an archive file Nsfilemanager *defaultmanager = [Nsfilemanager Defaultmanager]; if ([Defaultmanager isdeletablefileatpath:filename]) { [Defaultmanager removeitematpath:filename error:nil]; }
Cons: Archive the form to save data, only one-time archive save and one-time decompression. So only a small amount of data, and the data operation is clumsy, that is, if you want to change a small part of the data, still need to extract the entire data or archive the entire data.
Archive for iOS data storage (nskeyedarchiver)/Solution Files (nskeyedunarchiver)