使用NSData创建自定义档案

作者:追风剑情 发布于:2019-3-8 16:23 分类:Objective-C

示例:多个不同对象存储到同一个归档文件中

  1. #import <Foundation/Foundation.h>
  2.  
  3. @interface Foo : NSObject <NSCoding>
  4. @property (copy, nonatomic) NSString *strVal;
  5. @property int intVal;
  6. @property float floatVal;
  7.  
  8. -(void) encodeWithCoder: (NSCoder *) encoder;
  9. -(id) initWithCoder: (NSCoder *) decoder;
  10. @end
  11.  
  12. @implementation Foo
  13. @synthesize strVal, intVal, floatVal;
  14. -(void) encodeWithCoder: (NSCoder *) encoder
  15. {
  16. //[super encodeWithCoder: encoder];//如果有父类
  17. // key通常由类名+变量名组成
  18. [encoder encodeObject: strVal forKey: @"FoostrVal"];
  19. [encoder encodeInt: intVal forKey: @"FoointVal"];
  20. [encoder encodeFloat: floatVal forKey: @"FoofloatVal"];
  21. }
  22.  
  23. -(id) initWithCoder: (NSCoder *) decoder
  24. {
  25. strVal = [decoder decodeObjectForKey: @"FoostrVal"];
  26. intVal = [decoder decodeIntForKey: @"FoointVal"];
  27. floatVal = [decoder decodeFloatForKey: @"FoofloatVal"];
  28. return self;
  29. }
  30. @end
  31. //-------------------------Moo类----------------------------------
  32. @interface Moo : NSObject <NSCoding>
  33. @property (copy, nonatomic) NSString *strVal;
  34. @property int intVal;
  35. @property float floatVal;
  36.  
  37. -(void) encodeWithCoder: (NSCoder *) encoder;
  38. -(id) initWithCoder: (NSCoder *) decoder;
  39. @end
  40.  
  41. @implementation Moo
  42. @synthesize strVal, intVal, floatVal;
  43. -(void) encodeWithCoder: (NSCoder *) encoder
  44. {
  45. //[super encodeWithCoder: encoder];//如果有父类
  46. // key通常由类名+变量名组成
  47. [encoder encodeObject: strVal forKey: @"FoostrVal"];
  48. [encoder encodeInt: intVal forKey: @"FoointVal"];
  49. [encoder encodeFloat: floatVal forKey: @"FoofloatVal"];
  50. }
  51.  
  52. -(id) initWithCoder: (NSCoder *) decoder
  53. {
  54. strVal = [decoder decodeObjectForKey: @"FoostrVal"];
  55. intVal = [decoder decodeIntForKey: @"FoointVal"];
  56. floatVal = [decoder decodeFloatForKey: @"FoofloatVal"];
  57. return self;
  58. }
  59. @end
  60. //------------------------- Main -----------------------
  61. int main(int argc, const char * argv[]) {
  62. @autoreleasepool {
  63. Foo *myFoo = [[Foo alloc] init];
  64. Moo *myMoo = [[Moo alloc] init];
  65. NSMutableData *dataArea;
  66. NSKeyedArchiver *archiver;
  67. myFoo.strVal = @"[Foo] This is the string";
  68. myFoo.intVal = 100;
  69. myFoo.floatVal = 1.1;
  70. myMoo = [[Moo alloc] init];
  71. myMoo.strVal = @"[Moo] this is the string";
  72. myMoo.intVal = 200;
  73. myMoo.floatVal = 2.2;
  74. // 设置数据区,并将其连接到一个NSKeyedArchiver对象
  75. dataArea = [NSMutableData data];
  76. archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData: dataArea];
  77. // 开始存档
  78. [archiver encodeObject: myFoo forKey: @"myfoo"];
  79. [archiver encodeObject: myMoo forKey: @"mymoo"];
  80. [archiver finishEncoding];
  81. // 将存档的数据区写到文件
  82. if ([dataArea writeToFile: @"myArchive" atomically: YES] == NO)
  83. NSLog(@"Archiving failed!");
  84. //------------- 读取归档数据 ---------------
  85. NSKeyedUnarchiver *unarchiver;
  86. NSData *dataArea1;
  87. Foo *myFoo1;
  88. Moo *myMoo1;
  89. // 从归档文件中读取并连接NSKeyedUnarchiver对象
  90. dataArea1 = [NSData dataWithContentsOfFile: @"myArchive"];
  91. if (!dataArea1) {
  92. NSLog(@"Can't read back archive file!");
  93. return 1;
  94. }
  95. unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData: dataArea1];
  96. // 解码以前存储在归档文件中的对象
  97. myFoo1 = [unarchiver decodeObjectForKey: @"myfoo"];
  98. myMoo1 = [unarchiver decodeObjectForKey: @"mymoo"];
  99. [unarchiver finishDecoding];
  100. // 验证是否还原成功
  101. NSLog(@"\n [myFoo1] strVal=%@, intVal=%i, floatVal=%g",
  102. myFoo1.strVal, myFoo1.intVal, myFoo1.floatVal);
  103. NSLog(@"\n [myMoo1] strVal=%@, intVal=%i, floatVal=%g",
  104. myMoo1.strVal, myMoo1.intVal, myMoo1.floatVal);
  105. }
  106. return 0;
  107. }
运行测试
111.png


标签: Objective-C

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号