Objc object archive serialization

Source: Internet
Author: User

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.