Explanation of Objective-C archiving Problem Solving

Source: Internet
Author: User

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

 
 
  1. NSDictionary * glossay;
  2. // Save
  3. Glossay = [NSDictionary dictionaryWithObjectsAndKeys: @ "obj val 1", @ "key1", @ "obj val 2", @ "key2", nil];
  4. If ([glossay writeToFile: @ "glossary" atomically: YES] = NO ){
  5. NSLog (@ "Save to file failed! ");
  6. }
  7. // Obtain
  8. Glossay = [NSDictionary dictionaryWithContentsOfFile: @ "glossary"];
  9. NSLog (@ "% @", [glossay valueForKey: @ "key2"]);

Method 2: Use NSKeyedArchiver for archiving.

Code

 
 
  1. NSDictionary * glossay;
  2. Glossay = [NSDictionary dictionaryWithObjectsAndKeys: @ "obj val 1", @ "key1", @ "obj val 2", @ "key2", nil];
  3. // Save
  4. If ([NSKeyedArchiver archiveRootObject: glossay toFile: @ "glossay. archiver"] = NO ){
  5. NSLog (@ "write file fail !! ");
  6. }
  7. // Obtain
  8. Glossay = [NSKeyedUnarchiver unarchiveObjectWithFile: @ "glossay. archiver"];
  9. 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

 
 
  1. // TestProperty. h
  2. # Import <Cocoa/Cocoa. h>
  3. @ Interface TestProperty: NSObject <NSCopying, NSCoding> {
  4. NSString * name;
  5. NSString * password;
  6. NSMutableString * interest;
  7. NSInteger myInt;
  8. }
  9. 12 @ property (retain, nonatomic) NSString * name, * password;
  10. @ Property (retain, nonatomic) NSMutableString * interest;
  11. @ Property NSInteger myInt;
  12. -(Void) rename :( NSString *) newname;
  13. @ End
  14. ================================
  15. // TestProperty. m
  16. 23 # import "TestProperty. h"
  17. 25 26 @ implementation TestProperty
  18. 28 @ synthesize name, password, interest;
  19. @ Synthesize myInt;
  20. -(Void) rename :( NSString *) newname {
  21. // You can directly write it here
  22. // Self. name = newname;
  23. //
  24. If (name! = Newname ){
  25. [Name autorelease];
  26. Name = newname;
  27. [Name retain];
  28. }
  29. }
  30. -(Void) dealloc {
  31. Self. name = nil;
  32. Self. password = nil;
  33. Self. interest = nil;
  34. [Super dealloc];
  35. }
  36. -(Id) copyWithZone :( NSZone *) zone {
  37. TestProperty * newObj = [[self class] allocWithZone: zone] init];
  38. NewObj. name = name;
  39. NewObj. password = password;
  40. NewObj. myInt = myInt;
  41. // Deep copy
  42. NSMutableString * tmpStr = [interest mutableCopy];
  43. NewObj. interest = tmpStr;
  44. [TmpStr release];
  45. // Light copy
  46. // NewObj. interest = interest;
  47. Return newObj;
  48. }
  49. -(Void) encodeWithCoder :( NSCoder *) ACO {
  50. // If it is a subclass, add:
  51. // [Super encodeWithCoder: ACO];
  52. // Note that the NSCoding class is actually used to process objects )!
  53. [Ecoder encodeObject: name forKey: @ "TestPropertyName"];
  54. [Ecoder encodeObject: password forKey: @ "TestPropertyPassword"];
  55. [Ecoder encodeObject: interest forKey: @ "TestPropertyInterest"];
  56. // Note how to handle the basic type here!
  57. [Ecoder encodeInt: myInt forKey: @ "TestPropertyMyInt"];
  58. }
  59. -(Id) initWithCoder :( NSCoder *) aDecoder {
  60. // If it is a subclass, add:
  61. // Self = [super initWithCoder: aDecoder];
  62. // Decode the object
  63. Name = [[aDecoder decodeObjectForKey: @ "TestPropertyName"] retain];
  64. Password = [[aDecoder decodeObjectForKey: @ "TestPropertyPassword"] retain];
  65. Interest = [[aDecoder decodeObjectForKey: @ "TestPropertyInterest"] retain];
  66. // Basic decoding type
  67. MyInt = [aDecoder decodeIntForKey: @ "TestPropertyMyInt"];
  68. Return self;
  69. }
  70. @ End
  71. ====================
  72. // Test
  73. // Save
  74. TestProperty * test = [[TestProperty alloc] init];
  75. Test. name = @ "pxl ";
  76. Test. password = @ "pwd ...";
  77. Test. interest = [NSMutableString stringWithString: @ "interest..."];
  78. Test. myInt = 123;
  79. If ([NSKeyedArchiver archiveRootObject: test toFile: @ "testproerty. archive"] = NO ){
  80. NSLog (@ "write to file fail !! ");
  81. }
  82. // Obtain
  83. TestProperty * test = [NSKeyedUnarchiver unarchiveObjectWithFile: @ "testproerty. archive"];
  84. 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

 
 
  1. // Save
  2. TestProperty * test = [[TestProperty alloc] init];
  3. Test. name = @ "pxl ";
  4. Test. password = @ "pwd ...";
  5. Test. interest = [NSMutableString stringWithString: @ "interest..."];
  6. Test. myInt = 123;
  7. NSMutableData * dataArea = [NSMutableData data];
  8. NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData: dataArea];
  9. [Archiver encodeObject: test forKey: @ "testObj"];
  10. // You can add another object 13 //......
  11. [Archiver finishEncoding];
  12. If ([dataArea writeToFile: @ "test. archiver" atomically: YES] = NO ){
  13. NSLog (@ "write to file fail ...");
  14. }
  15. [Archiver release];
  16. [Test release];
  17. ================
  18. // Obtain
  19. NSData * dataArea = [NSData dataWithContentsOfFile: @ "test. archiver"];
  20. If (! DataArea ){
  21. NSLog (@ "Can't read back archive file ");
  22. Return (1 );
  23. }
  24. NSKeyedUnarchiver * unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData: dataArea];
  25. TestProperty * test = [unarchiver decodeObjectForKey: @ "testObj"];
  26. [Unarchiver finishDecoding];
  27. NSLog (@ "% @", test. name );
  28. [Unarchiver release];

Object deep Replication Using archive:

Code

 
 
  1. // Delete the NSCopying protocol Code implemented in the TestProperty class first.
  2. TestProperty * test = [[TestProperty alloc] init];
  3. Test. name = @ "pxl ";
  4. Est. password = @ "pwd ...";
  5. Test. interest = [NSMutableString stringWithString: @ "interest..."];
  6. Test. myInt = 123;
  7. // Perform a deep copy of test 10 NSData * data = [NSKeyedArchiver archivedDataWithRootObject: test]; 11
  8. TestProperty * test2 = [NSKeyedUnarchiver unarchiveObjectWithData: data];
  9. [Test2.interest appendString: @ "film"];
  10. NSLog (@ "% @", test. interest); 15 NSLog (@ "% @", test2.interest );
  11. // Output
  12. 16:11:47. 391 HelloWorld [4599: a0f] interest...
  13. 16:11:47. 393 HelloWorld [4599: a0f] interest... film

Summary: DetailsObjective-C ArchiveAfter the problem is solved, I hope this article will help you!

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.