【IPHONE开发-OBJECTC入门学习】对象的归档和解归档

转自:http://blog.csdn.net/java886o/article/details/9046967

  1. #import <Foundation/Foundation.h>
  2. #import "Person.h"
  3. int main(int argc, const char * argv[])
  4. {
  5. @autoreleasepool {
  6. //-----------第1种归档方式---------
  7. //1.使用NSKeyedArchiver 归档对象到文件(对象序列化,持久化)
  8. NSArray* arrays = [[NSArray alloc]initWithObjects:@"111",@"222", nil];
  9. NSString* userPath = NSHomeDirectoryForUser(@"3g2win");
  10. NSLog(@"userPath=%@",userPath);
  11. NSString* bakFilePath = [userPath stringByAppendingFormat:@"/test.txt"];
  12. NSLog(@"bakFilePath=%@",bakFilePath);
  13. NSLog(@"归档前的数组:\n\n%@",arrays);
  14. //归档
  15. if ([NSKeyedArchiver archiveRootObject:arrays toFile:bakFilePath]){
  16. NSLog(@"归档成功...\n\n");
  17. }else {
  18. NSLog(@"归档失败...\n\n");
  19. }
  20. //2.使用NSKeyedUnarchiver 解归档文件到对象(反序列化)
  21. //解归档
  22. NSArray* srcArr =  [NSKeyedUnarchiver unarchiveObjectWithFile:bakFilePath];
  23. NSLog(@"解归档后的数组:\n\n%@",srcArr);
  24. //------------第2种归档方式---------
  25. //归档
  26. NSMutableData* data = [NSMutableData alloc];
  27. NSKeyedArchiver* archiver = [NSKeyedArchiver alloc];
  28. [archiver initForWritingWithMutableData:data];
  29. [archiver encodeObject:@"张三" forKey:@"name"];
  30. [archiver encodeInt:25 forKey:@"age"];
  31. [archiver encodeFloat:5200.5F forKey:@"money"];
  32. [archiver finishEncoding];
  33. [data writeToFile:@"/Users/3g2win/11111111111.txt" atomically:YES];
  34. //解归档
  35. NSMutableData* data2 = [NSMutableData dataWithContentsOfFile:@"/Users/3g2win/11111111111.txt"];
  36. NSKeyedUnarchiver* unArchiver = [NSKeyedUnarchiver alloc];
  37. [unArchiver initForReadingWithData:data2];
  38. NSLog(@"name=%@",[unArchiver decodeObjectForKey:@"name"]);
  39. NSLog(@"age=%d",[unArchiver decodeIntForKey:@"age"]);
  40. NSLog(@"money=%f",[unArchiver decodeFloatForKey:@"money"]);
  41. //-------------自定义对象的归档和解归档------------
  42. Person* zhao6 = [[Person alloc] initWithName:@"赵六" withAge:18];
  43. [zhao6 display];
  44. //归档
  45. [NSKeyedArchiver archiveRootObject:zhao6 toFile:@"/Users/3g2win/zhao6.txt"];
  46. //解档
  47. Person* newZhao6 = [NSKeyedUnarchiver unarchiveObjectWithFile:@"/Users/3g2win/zhao6.txt"];
  48. [newZhao6 display];
  49. }
  50. return 0;
  51. }
#import <Foundation/Foundation.h>
#import "Person.h" int main(int argc, const char * argv[])
{ @autoreleasepool { //-----------第1种归档方式--------- //1.使用NSKeyedArchiver 归档对象到文件(对象序列化,持久化)
NSArray* arrays = [[NSArray alloc]initWithObjects:@"111",@"222", nil]; NSString* userPath = NSHomeDirectoryForUser(@"3g2win"); NSLog(@"userPath=%@",userPath); NSString* bakFilePath = [userPath stringByAppendingFormat:@"/test.txt"]; NSLog(@"bakFilePath=%@",bakFilePath); NSLog(@"归档前的数组:\n\n%@",arrays); //归档 if ([NSKeyedArchiver archiveRootObject:arrays toFile:bakFilePath]){
NSLog(@"归档成功...\n\n");
}else {
NSLog(@"归档失败...\n\n");
} //2.使用NSKeyedUnarchiver 解归档文件到对象(反序列化)
//解归档
NSArray* srcArr = [NSKeyedUnarchiver unarchiveObjectWithFile:bakFilePath]; NSLog(@"解归档后的数组:\n\n%@",srcArr); //------------第2种归档方式--------- //归档
NSMutableData* data = [NSMutableData alloc];
NSKeyedArchiver* archiver = [NSKeyedArchiver alloc];
[archiver initForWritingWithMutableData:data];
[archiver encodeObject:@"张三" forKey:@"name"];
[archiver encodeInt:25 forKey:@"age"];
[archiver encodeFloat:5200.5F forKey:@"money"];
[archiver finishEncoding];
[data writeToFile:@"/Users/3g2win/11111111111.txt" atomically:YES]; //解归档
NSMutableData* data2 = [NSMutableData dataWithContentsOfFile:@"/Users/3g2win/11111111111.txt"];
NSKeyedUnarchiver* unArchiver = [NSKeyedUnarchiver alloc];
[unArchiver initForReadingWithData:data2]; NSLog(@"name=%@",[unArchiver decodeObjectForKey:@"name"]); NSLog(@"age=%d",[unArchiver decodeIntForKey:@"age"]); NSLog(@"money=%f",[unArchiver decodeFloatForKey:@"money"]); //-------------自定义对象的归档和解归档------------
Person* zhao6 = [[Person alloc] initWithName:@"赵六" withAge:18]; [zhao6 display]; //归档
[NSKeyedArchiver archiveRootObject:zhao6 toFile:@"/Users/3g2win/zhao6.txt"]; //解档 Person* newZhao6 = [NSKeyedUnarchiver unarchiveObjectWithFile:@"/Users/3g2win/zhao6.txt"]; [newZhao6 display]; }
return 0;
}

Person.h

  1. #import <Foundation/Foundation.h>
  2. //必须实现NSCoding协议才能够归档解归档自定义类
  3. @interface Person : NSObject <NSCoding>
  4. {
  5. NSString* name;
  6. int age;
  7. }
  8. @property (nonatomic,assign) NSString* name;
  9. @property (nonatomic,assign) int age;
  10. - (id) initWithName:(NSString*) _name withAge:(int) _age;
  11. - (void) display;
  12. @end
#import <Foundation/Foundation.h>

//必须实现NSCoding协议才能够归档解归档自定义类

@interface Person : NSObject <NSCoding>

{
NSString* name;
int age;
} @property (nonatomic,assign) NSString* name;
@property (nonatomic,assign) int age; - (id) initWithName:(NSString*) _name withAge:(int) _age; - (void) display; @end

Person.m

    1. #import "Person.h"
    2. #define NAME @"NAME"
    3. #define AGE @"AGE"
    4. @implementation Person
    5. @synthesize name;
    6. @synthesize age;
    7. - (id) initWithName:(NSString*) _name withAge:(int) _age {
    8. if (self = [super init]) {
    9. self.name = _name;
    10. self.age = _age;
    11. }
    12. return self;
    13. }
    14. - (void) display {
    15. NSLog(@"Person Name : %@\t Age :%d",name,age);
    16. }
    17. //归档编码
    18. - (void)encodeWithCoder:(NSCoder *)aCoder {
    19. [aCoder encodeObject:name forKey:NAME];
    20. [aCoder encodeInt:age forKey:AGE];
    21. }
    22. //解归档解码
    23. - (id)initWithCoder:(NSCoder *)aDecoder {
    24. if (self = [super init]) {
    25. self.name = [aDecoder decodeObjectForKey:NAME];
    26. self.age = [aDecoder decodeIntForKey:AGE];
    27. }
    28. return self;
    29. }
    30. @end
上一篇:Java基础:(五)Object通用方法


下一篇:website architecture