NSString, NSArray, NSData, and NSDictionary all implement the NSCoding protocol and can be directly archived by calling writeToFile. What about the OBJC custom object type? First, implement the NSCoding protocol, rewrite the encodeWithCode method and the initWithCode method, convert to NSData through NSKeyedArchiver, and then write to the file through the writeToFile method of NSData, you can also put the converted NSData into NSArray or NSDictionary and call writeToFile to write it to the file to archive custom data and dictionaries. You can use NSKeyedUnarchiver to read the archive file to the object, or use the arrrayWithContentsOfFile of NSArray or the dictionaryWithContentsOfFile of NSDictionary to the array object or dictionary, and then retrieve the serialized custom object (that is, the NSData form of the custom object ), then archive the object through NSKeyedUnarchiver.
[Plain]
------------------------------------------------ Test code ------------------------------------------------
NSString * str = @ "hello ";
[Str writeToFile: @ "/Users/lili/Desktop/str.txt" atomically: YES encoding: NSUTF8StringEncoding error: nil];
NSArray * arr = [[NSArray alloc] initWithObjects: @ "lili", @ "tata", nil];
[Arr writeToFile: @ "/Users/lili/Desktop/arr.txt" atomically: YES];
NSDictionary * dict = [NSDictionary dictionaryWithObjects: @ [@ "lili", @ "male"] forKeys: @ [@ "name", @ "sex"];
[Dict writeToFile: @ "/Users/lili/Desktop/dict.txt" atomically: YES];
Person * parent = [[Person alloc] init];
Parent. age = 40;
Parent. name = @ "lili ";
Person * child = [[Person alloc] init];
Child. age = 22;
Child. name = @ "linian ";
Parent. child = child;
NSData * data = [NSKeyedArchiver archivedDataWithRootObject: parent];
[Data writeToFile: @ "/Users/lili/Desktop/data.txt" atomically: YES];
Person * people = [NSKeyedUnarchiver unarchiveObjectWithFile: @ "/Users/lili/Desktop/data.txt"];
NSLog (@ "% @", people );
NSDictionary * dictPerson = [NSDictionary Syntax: @ [[NSKeyedArchiver Syntax: parent], [NSKeyedArchiver Syntax: child] forKeys: @ [@ "parent", @ "child"];
[DictPerson writeToFile: @ "/Users/lili/Desktop/dictPerson.txt" atomically: YES];
NSDictionary * pdict = [NSDictionary dictionaryWithContentsOfFile: @ "/Users/lili/Desktop/dictPerson.txt"];
Person * tPerson = [NSKeyedUnarchiver unarchiveObjectWithData: [pdict objectForKey: @ "parent"];
NSLog (@ "% @", tPerson );
NSArray * arrPerson = [[NSArray alloc] initWithObjects: [NSKeyedArchiver archivedDataWithRootObject: parent], [NSKeyedArchiver parent: parent], nil];
[ArrPerson writeToFile: @ "/Users/lili/Desktop/arrPerson.txt" atomically: YES];
NSArray * pArr = [NSArray arrayWithContentsOfFile: @ "/Users/lili/Desktop/arrPerson.txt"];
Person * aPerson = [NSKeyedUnarchiver unarchiveObjectWithData: [pArr objectAtIndex: 0];
NSLog (@ "% @", aPerson );
------------------------------------------------ Person class ------------------------------------------------
# Import <Foundation/Foundation. h>
@ Interface Person: NSObject <NSCoding>
@ Property (strong, nonatomic) Person * child;
@ Property (assign, nonatomic) int age;
@ Property (copy, nonatomic) NSString * name;
@ End
# Import "Person. h"
@ Implementation Person
-(NSString *) description
{
Return [NSString stringWithFormat: @ "{age: % d, name: % @, child: % @}", self. age, self. name, self. child];
}
-(Void) encodeWithCoder :( NSCoder *) ACO
{
[Ecoder encodeInt: self. age forKey: @ "age"];
[Ecoder encodeObject: self. name forKey: @ "name"];
[Ecoder encodeObject: self. child forKey: @ "child"];
}
-(Id) initWithCoder :( NSCoder *) aDecoder
{
If (self = [super init]) {
Self. age = [aDecoder decodeIntForKey: @ "age"];
Self. name = [aDecoder decodeObjectForKey: @ "name"];
Self. child = [aDecoder decodeObjectForKey: @ "child"];
}
Return self;
}
@ End
------------------------------------------------ Test code ------------------------------------------------
NSString * str = @ "hello ";
[Str writeToFile: @ "/Users/lili/Desktop/str.txt" atomically: YES encoding: NSUTF8StringEncoding error: nil];
NSArray * arr = [[NSArray alloc] initWithObjects: @ "lili", @ "tata", nil];
[Arr writeToFile: @ "/Users/lili/Desktop/arr.txt" atomically: YES];
NSDictionary * dict = [NSDictionary dictionaryWithObjects: @ [@ "lili", @ "male"] forKeys: @ [@ "name", @ "sex"];
[Dict writeToFile: @ "/Users/lili/Desktop/dict.txt" atomically: YES];
Person * parent = [[Person alloc] init];
Parent. age = 40;
Parent. name = @ "lili ";
Person * child = [[Person alloc] init];
Child. age = 22;
Child. name = @ "linian ";
Parent. child = child;
NSData * data = [NSKeyedArchiver archivedDataWithRootObject: parent];
[Data writeToFile: @ "/Users/lili/Desktop/data.txt" atomically: YES];
Person * people = [NSKeyedUnarchiver unarchiveObjectWithFile: @ "/Users/lili/Desktop/data.txt"];
NSLog (@ "% @", people );
NSDictionary * dictPerson = [NSDictionary Syntax: @ [[NSKeyedArchiver Syntax: parent], [NSKeyedArchiver Syntax: child] forKeys: @ [@ "parent", @ "child"];
[DictPerson writeToFile: @ "/Users/lili/Desktop/dictPerson.txt" atomically: YES];
NSDictionary * pdict = [NSDictionary dictionaryWithContentsOfFile: @ "/Users/lili/Desktop/dictPerson.txt"];
Person * tPerson = [NSKeyedUnarchiver unarchiveObjectWithData: [pdict objectForKey: @ "parent"];
NSLog (@ "% @", tPerson );
NSArray * arrPerson = [[NSArray alloc] initWithObjects: [NSKeyedArchiver archivedDataWithRootObject: parent], [NSKeyedArchiver parent: parent], nil];
[ArrPerson writeToFile: @ "/Users/lili/Desktop/arrPerson.txt" atomically: YES];
NSArray * pArr = [NSArray arrayWithContentsOfFile: @ "/Users/lili/Desktop/arrPerson.txt"];
Person * aPerson = [NSKeyedUnarchiver unarchiveObjectWithData: [pArr objectAtIndex: 0];
NSLog (@ "% @", aPerson );
------------------------------------------------ Person class ------------------------------------------------
# Import <Foundation/Foundation. h>
@ Interface Person: NSObject <NSCoding>
@ Property (strong, nonatomic) Person * child;
@ Property (assign, nonatomic) int age;
@ Property (copy, nonatomic) NSString * name;
@ End
# Import "Person. h"
@ Implementation Person
-(NSString *) description
{
Return [NSString stringWithFormat: @ "{age: % d, name: % @, child: % @}", self. age, self. name, self. child];
}
-(Void) encodeWithCoder :( NSCoder *) ACO
{
[Ecoder encodeInt: self. age forKey: @ "age"];
[Ecoder encodeObject: self. name forKey: @ "name"];
[Ecoder encodeObject: self. child forKey: @ "child"];
}
-(Id) initWithCoder :( NSCoder *) aDecoder
{
If (self = [super init]) {
Self. age = [aDecoder decodeIntForKey: @ "age"];
Self. name = [aDecoder decodeObjectForKey: @ "name"];
Self. child = [aDecoder decodeObjectForKey: @ "child"];
}
Return self;
}
@ End