iOS培训之数据保持(二)

作者:达内 更新时间:2014-06-17 11:59 来源:未知 点击:
模型对象归档: NSString、NSArray、NSData、NSDictionary都实现了NSCoding协议,可直接通过调用writeToFile归档。 但如果想保持对象类型的数据,则需要到该方法灵活实现。
模型对象归档:
NSString、NSArray、NSData、NSDictionary都实现了NSCoding协议,可直接通过调用writeToFile归档。
   但如果想保持对象类型的数据,则需要到该方法灵活实现。首先可将需归档的对象先实现NSCoding协议,重写
 
encodeWithCode方法和initWithCode方法,然后通过NSKeyedArchiver转换为NSData,然后通过NSData的writeToFile方
 
法写入到文件,或者将转换后的NSData放入到NSArray或NSDictionary中调用writeToFile写入到文件便可实现包装了自
 
定义类型的数据和字典的归档;
   通过NSKeyedUnarchiver读取归档文件到对象,或者通过NSArray的arrrayWithContentsOfFile或NSDictionary的
 
dictionaryWithContentsOfFile到数组对象或字典,然后取出序列化过的自定义对象(即自定义对象的NSData形式),
 
然后通过NSKeyedUnarchiver反归档到对象。
根视图控制器:
#define kFilename @"archive" 
#define kDataKey @"Data" 
-(NSString *)dataFilePath 
 {
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory=[paths objectAtIndex:0];         return [documentsDirectory 
 
stringByAppendingPathComponent:kFilename]; 
 } 
 
- (void)viewDidLoad 
 { 
    [super viewDidLoad]; 
if ([[NSFileManager defaultManager] fileExistsAtPath:[self dataFilePath]]) 
{
    NSMutableData *data=[[NSMutableData alloc] initWithContentsOfFile:[self dataFilePath]]; 
    NSKeyedUnarchiver *unarchiver=[[NSKeyedUnarchiver alloc] initForReadingWithData:data]; 
    BIDFourLines *fourlines=[unarchiver         decodeObjectForKey:kDataKey]; 
    [unarchiver finishDecoding]; 
    field1.text=fourlines.field1; 
    field2.text=fourlines.field2; 
    field3.text=fourlines.field3; 
    field4.text=fourlines.field4;
 
    UIApplication *app =[UIApplication sharedApplication];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector
 
(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:app]; 
 
-(void)applicationWillResignActive:(NSNotification *)notification;
    BIDFourLines *fourlines=[[BIDFourLines alloc] init];
    fourlines.field1=field1.text; 
    fourlines.field2=field2.text; 
    fourlines.field3=field3.text; 
    fourlines.field4=field4.text;
    NSMutableData *data=[[NSMutableData alloc] init]; 
    NSKeyedArchiver *archiver=[[NSKeyedArchiver alloc] initForWritingWithMutableData:data];//将archiver在
 
收到编码后数据自动转为二进制写到data 
    [archiver encodeObject:fourlines forKey:kDataKey];//以Data为键编码 
    [archiver finishEncoding]; 
    [data writeToFile:[self dataFilePath] atomically:YES]; //将data写入Documents中的archiver中。
}
/*
writeToFile:
这方法其实是写到plist文件(生成plist文件中的一个档)中的,根据官方文档描述If the array’s contents are 
 
all property list objects (NSString, NSData, NSArray, or NSDictionary objects), the file written by this 
 
method can be used to initialize a new array with the class method arrayWithContentsOfFile: or the 
 
instance method initWithContentsOfFile:. This method recursively validates that all the contained objects 
 
are property list objects before writing out the file, and returns NO if all the objects are not property 
 
list objects, since the resultant file would not be a valid property list.
*/
模型对象:
 1 #pragma mark NSCoding
 2 -(void)encodeWithCoder:(NSCoder *)aCoder
 3 {
 4     [aCoder encodeObject:field1 forKey:kField1Key];
 5     [aCoder encodeObject:field2 forKey:kField2Key];
 6     [aCoder encodeObject:field3 forKey:kField3Key];
 7     [aCoder encodeObject:field4 forKey:kField4Key];
 8 }
 9  
10 -(id)initWithCoder:(NSCoder *)aDecoder
11 {
12     if(self=[super init])
13     {
14         self.field1=[aDecoder decodeObjectForKey:kField1Key];
15         self.field2=[aDecoder decodeObjectForKey:kField2Key];
16         self.field3=[aDecoder decodeObjectForKey:kField3Key];
17         self.field4=[aDecoder decodeObjectForKey:kField4Key];
18     }
19     return self;
20 }
21  
22 #pragma mark NSCopying
23 -(id)copyWithZone:(NSZone *)zone
24 {
25     BIDFourLines *copy=[[[self class] allocWithZone:zone] init];
26     copy.field1=[self.field1 copyWithZone:zone];
27     copy.field2=[self.field2 copyWithZone:zone];
28     copy.field3=[self.field3 copyWithZone:zone];
29     copy.field4=[self.field4 copyWithZone:zone];
30     
31     return copy;
32 } 
沙盒中的Documents文件夹生成archiver文件(无扩展名,加上.plist扩展名即可查看编码后保存的数据内容)。
其实,实现上面的功能并不复杂,大体的只有以上的步骤,算不上繁琐,只要细心都不会出现什么问题的,所以学习技
 
术要有一定的细心和信心,什么都可以做得很好的。 
标签:

相关阅读

最新开班信息

3G-IOS软件工程师就业班
中关村校区上课 开课日期:6月30日
3G-IOS软件工程师就业班
中关村校区上课 开课日期:9月29日
3G-IOS软件工程师周末班
中关村校区上课 开课日期:6月30日
3G-IOS软件工程师周末班
中关村校区上课 开课日期:9月29日