Objective-C ArchiveThe problem is solved in this article, which focuses on learning how to use Objective-C.Archive, This article solves this problem in detail, let's look at the details.
For basic Objective-C class objects (NSString, NSArray ...):
Method 1: Use the XML Attribute list for archiving.
Code
- NSDictionary * glossay;
- // Save
- Glossay = [NSDictionary dictionaryWithObjectsAndKeys: @ "obj val 1", @ "key1", @ "obj val 2", @ "key2", nil];
- If ([glossay writeToFile: @ "glossary" atomically: YES] = NO ){
- NSLog (@ "Save to file failed! ");
- }
- // Obtain
- Glossay = [NSDictionary dictionaryWithContentsOfFile: @ "glossary"];
- NSLog (@ "% @", [glossay valueForKey: @ "key2"]);
Method 2: Use NSKeyedArchiver for archiving.
Code
- NSDictionary * glossay;
- Glossay = [NSDictionary dictionaryWithObjectsAndKeys: @ "obj val 1", @ "key1", @ "obj val 2", @ "key2", nil];
- // Save
- If ([NSKeyedArchiver archiveRootObject: glossay toFile: @ "glossay. archiver"] = NO ){
- NSLog (@ "write file fail !! ");
- }
- // Obtain
- Glossay = [NSKeyedUnarchiver unarchiveObjectWithFile: @ "glossay. archiver"];
- NSLog (@ "% @", [glossay valueForKey: @ "key2"]);
For a custom Class, you need to implement the NSCoding protocol, and then use the above method 2 for archiving:
Code
- // TestProperty. h
- # Import <Cocoa/Cocoa. h>
- @ Interface TestProperty: NSObject <NSCopying, NSCoding> {
- NSString * name;
- NSString * password;
- NSMutableString * interest;
- NSInteger myInt;
- }
- 12 @ property (retain, nonatomic) NSString * name, * password;
- @ Property (retain, nonatomic) NSMutableString * interest;
- @ Property NSInteger myInt;
- -(Void) rename :( NSString *) newname;
- @ End
- ================================
- // TestProperty. m
- 23 # import "TestProperty. h"
- 25 26 @ implementation TestProperty
- 28 @ synthesize name, password, interest;
- @ Synthesize myInt;
- -(Void) rename :( NSString *) newname {
- // You can directly write it here
- // Self. name = newname;
- //
- If (name! = Newname ){
- [Name autorelease];
- Name = newname;
- [Name retain];
- }
- }
- -(Void) dealloc {
- Self. name = nil;
- Self. password = nil;
- Self. interest = nil;
- [Super dealloc];
- }
- -(Id) copyWithZone :( NSZone *) zone {
- TestProperty * newObj = [[self class] allocWithZone: zone] init];
- NewObj. name = name;
- NewObj. password = password;
- NewObj. myInt = myInt;
- // Deep copy
- NSMutableString * tmpStr = [interest mutableCopy];
- NewObj. interest = tmpStr;
- [TmpStr release];
- // Light copy
- // NewObj. interest = interest;
- Return newObj;
- }
- -(Void) encodeWithCoder :( NSCoder *) ACO {
- // If it is a subclass, add:
- // [Super encodeWithCoder: ACO];
- // Note that the NSCoding class is actually used to process objects )!
- [Ecoder encodeObject: name forKey: @ "TestPropertyName"];
- [Ecoder encodeObject: password forKey: @ "TestPropertyPassword"];
- [Ecoder encodeObject: interest forKey: @ "TestPropertyInterest"];
- // Note how to handle the basic type here!
- [Ecoder encodeInt: myInt forKey: @ "TestPropertyMyInt"];
- }
- -(Id) initWithCoder :( NSCoder *) aDecoder {
- // If it is a subclass, add:
- // Self = [super initWithCoder: aDecoder];
- // Decode the object
- Name = [[aDecoder decodeObjectForKey: @ "TestPropertyName"] retain];
- Password = [[aDecoder decodeObjectForKey: @ "TestPropertyPassword"] retain];
- Interest = [[aDecoder decodeObjectForKey: @ "TestPropertyInterest"] retain];
- // Basic decoding type
- MyInt = [aDecoder decodeIntForKey: @ "TestPropertyMyInt"];
- Return self;
- }
- @ End
- ====================
- // Test
- // Save
- TestProperty * test = [[TestProperty alloc] init];
- Test. name = @ "pxl ";
- Test. password = @ "pwd ...";
- Test. interest = [NSMutableString stringWithString: @ "interest..."];
- Test. myInt = 123;
- If ([NSKeyedArchiver archiveRootObject: test toFile: @ "testproerty. archive"] = NO ){
- NSLog (@ "write to file fail !! ");
- }
- // Obtain
- TestProperty * test = [NSKeyedUnarchiver unarchiveObjectWithFile: @ "testproerty. archive"];
- NSLog (@ "% @", test. name );
Use NSData to create a definition file.
The preceding TestProperty class that has implemented the NSCoding protocol is used as an example. The Code
- // Save
- TestProperty * test = [[TestProperty alloc] init];
- Test. name = @ "pxl ";
- Test. password = @ "pwd ...";
- Test. interest = [NSMutableString stringWithString: @ "interest..."];
- Test. myInt = 123;
- NSMutableData * dataArea = [NSMutableData data];
- NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData: dataArea];
- [Archiver encodeObject: test forKey: @ "testObj"];
- // You can add another object 13 //......
- [Archiver finishEncoding];
- If ([dataArea writeToFile: @ "test. archiver" atomically: YES] = NO ){
- NSLog (@ "write to file fail ...");
- }
- [Archiver release];
- [Test release];
- ================
- // Obtain
- NSData * dataArea = [NSData dataWithContentsOfFile: @ "test. archiver"];
- If (! DataArea ){
- NSLog (@ "Can't read back archive file ");
- Return (1 );
- }
- NSKeyedUnarchiver * unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData: dataArea];
- TestProperty * test = [unarchiver decodeObjectForKey: @ "testObj"];
- [Unarchiver finishDecoding];
- NSLog (@ "% @", test. name );
- [Unarchiver release];
Object deep Replication Using archive:
Code
- // Delete the NSCopying protocol Code implemented in the TestProperty class first.
- TestProperty * test = [[TestProperty alloc] init];
- Test. name = @ "pxl ";
- Est. password = @ "pwd ...";
- Test. interest = [NSMutableString stringWithString: @ "interest..."];
- Test. myInt = 123;
- // Perform a deep copy of test 10 NSData * data = [NSKeyedArchiver archivedDataWithRootObject: test]; 11
- TestProperty * test2 = [NSKeyedUnarchiver unarchiveObjectWithData: data];
- [Test2.interest appendString: @ "film"];
- NSLog (@ "% @", test. interest); 15 NSLog (@ "% @", test2.interest );
- // Output
- 16:11:47. 391 HelloWorld [4599: a0f] interest...
- 16:11:47. 393 HelloWorld [4599: a0f] interest... film
Summary: DetailsObjective-C ArchiveAfter the problem is solved, I hope this article will help you!