文件加载和保存

来源:互联网 发布:福州seo技巧培训 编辑:程序博客网 时间:2024/06/06 01:08

原文链接;http://blog.csdn.net/yhawaii/article/details/7380559

1、使用属性列表保存对象:
在Cocoa中,与一类名为属性列表的对象,常简称为plist。这些列表包含Cocoa知道如何操作的一组对象。具体来讲,Cocoa知道如何将它们保存到文件中并进行加载。属性列表类包括:NSArray,NSDictionary,NSString和NSData,以及它们的变体(Mutable)

eg:

[plain] view plaincopy
  1. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  
  2. NSArray *array = [NSArray arrayWithObjects:@"First",  
  3.                               @"second",@"third",@"fourth",@"fifth",nil];  
  4. [array writeToFile:@"array.plist" atomically:YES];   

2、编码对象
遗憾的是,无法总是将对象信息表示为属性列表类。如果能将所有对象都表示为数组字典,我们就没有必要使用自己的类了。所幸,Cocoa具备一种机制来将对象自身转化为某种格式并保存到磁盘中。对象可以将它们的实例变量和其它数据编码为数据块,然后保存到磁盘中。遗憾将这些数据块读到内存中,并且还能基于保存的数据创建新对象。这个过程称为编码和解码,或称为序列化和反序列化。
    通过NSCoding协议,可以使用自己的对象实现相同功能,实现它的两个方法: 
- (void)encodeWithCoder:(NSCoder *)aCoder;
- (id)initWithCoder:(NSCoder *)aDecoder;
NSCoder是一个抽象类,定义一些有用的方法来在对象与NSData之间来回转换。完全不需要创建新NSCoder,因为它事件上并无多大作用。但是我们实际上要使用NSCoder的一些具体子类来编码和解码对象。我们将使用其中两个子类NSKeyedArchiver和NSKeyedUnArchiver.
下面是一个例子:
头文件类BookObj.h的源码:
[plain] view plaincopy
  1. //  
  2. //  BookObj.h  
  3.   
  4. //  
  5.   
  6. #import <Cocoa/Cocoa.h>  
  7. @interface BookObj:NSObject<NSCoding>{  
  8.     NSString *bookName;  
  9.     NSString *author;  
  10. }  
  11.   
  12. @property (copy) NSString *bookName;  
  13. @property (copy) NSString *author;  
  14.   
  15.   
  16. -(id)initWithName:(NSString *)name  
  17.            author:(NSString *) au ;  
  18.   
  19. @end  

实现类BookObj.m的源码:

[plain] view plaincopy
  1. //  
  2. //  BookObj.m  
  3.   
  4. //  
  5. #import "BookObj.h"  
  6.   
  7.   
  8. @implementation BookObj  
  9.   
  10. @synthesize bookName;  
  11. @synthesize author;  
  12. -(id)initWithName:(NSString *)name  
  13.            author:(NSString *) au{  
  14.     if (self = [super init]) {  
  15.         self.bookName = name;  
  16.         self.author = au;  
  17.     }  
  18.     return self;  
  19. }  
  20.   
  21. - (void)encodeWithCoder:(NSCoder *)aCoder{  
  22.     [aCoder encodeObject:self.bookName forKey:@"bookName"];  
  23.     [aCoder encodeObject:self.author    forKey:@"author"];  
  24. }  
  25. - (id)initWithCoder:(NSCoder *)aDecoder{  
  26.     if (self =[super init]) {  
  27.         self.bookName = [aDecoder decodeObjectForKey:@"bookName"];  
  28.         self.author = [aDecoder decodeObjectForKey:@"author"];  
  29.     }  
  30.     return self;  
  31. }  
  32.   
  33.   
  34. int main(int argc ,const char *argv[]){  
  35.     BookObj *bookObj = [[BookObj alloc] initWithName:@"iPhone编程指南"  
  36.                                               author:@"David"];  
  37.       
  38.     [NSKeyedArchiver archiveRootObject:bookObj toFile:@"bookObj.plist"];  
  39.     NSLog(@"Success to archive file bookObj.plist!");  
  40.     BookObj *bookOb =   [NSKeyedUnarchiver unarchiveObjectWithFile:@"bookObj.plist"];  
  41.       
  42.     NSLog(@"The book name is :%@",bookOb.author);  
  43.     return 0;  
  44. }  
  45. @end  

0 0