码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 开发指南 第11章 数据持久化之对象归档 学习

时间:2015-09-19 12:18:44      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:

1 是一种序列化方式,先将归档对象序列化为一个文件,然后再通过反归档将数据恢复到对象中。

   条件:该对象的类必须实现NSCoding协议,而且每个成员变量应该是基本数据类型或都是实现NSCoding协议的某个类的实例。

   归档类NSKeyedArchiver 反归档类NSKeyedUnarchiver  NSData类提供了读取数据文件的方法

   方法:+dataWithContentsOfFile:

           +dataWithContentsOfFile:options:指定读取数据选项error:

           -initWithContentsOfFile:/options:error:

           -writeToFile:atomically:若文件不存在在这时候创建一个

           -writeToFile:options:error:

2 归档:封装NSData对象-封装NSKeyedArchiver对象-编码-完成编码-写入文件

- (void)createEditableCopyOfDatabaseIfNeeded {
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *writableDBPath = [self applicationDocumentsDirectoryFile];
    
    BOOL dbexits = [fileManager fileExistsAtPath:writableDBPath];
    if (!dbexits) {
        
        NSString *path = [self applicationDocumentsDirectoryFile];
        
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        
        NSDate *date1 = [dateFormatter dateFromString:@"2010-08-04 16:01:03"];
        Note* note1 = [[Note alloc] init];
        note1.date = date1;
        note1.content = @"Welcome to MyNote.";
        NSMutableArray* array  = [[NSMutableArray alloc] init];
        [array addObject:note1];
        
        
        NSMutableData * theData = [NSMutableData data];
        NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc]
                                      initForWritingWithMutableData:theData];
        [archiver encodeObject:array forKey:ARCHIVE_KEY];里面的Note对象必须是实现NSCoding协议
        [archiver finishEncoding];
        
        [theData writeToFile:path atomically:YES];
    }
}

- (NSString *)applicationDocumentsDirectoryFile {
    NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *path = [documentDirectory stringByAppendingPathComponent:FILE_NAME];
    return path;
}

Note类必须实现NSCoding协议:

 

-(void)encodeWithCoder:(NSCoder *)aCoder {编码
    [aCoder encodeObject:_date forKey:@"date"];
    [aCoder encodeObject:_content forKey:@"content"];
}

-(id)initWithCoder:(NSCoder *)aDecoder {反编码
    self.date = [aDecoder decodeObjectForKey:@"date"];
    self.content = [aDecoder decodeObjectForKey:@"content"];
    return self;
}

 

3 反归档:查询

//查询所有数据方法
-(NSMutableArray*) findAll
{
    NSString *path = [self applicationDocumentsDirectoryFile];
    
    NSMutableArray *listData = [[NSMutableArray alloc] init];
    NSData * theData =[NSData dataWithContentsOfFile:path];
    
    if([theData length] > 0) {判断归档对象是否有数据
        NSKeyedUnarchiver * archiver = [[NSKeyedUnarchiver alloc]
                                        initForReadingWithData:theData];
        listData = [archiver decodeObjectForKey:ARCHIVE_KEY];
        [archiver finishDecoding];

    }
    return listData;
}

//按照主键查询数据方法
-(Note*) findById:(Note*)model
{
    NSString *path = [self applicationDocumentsDirectoryFile];
    
    NSMutableArray *listData = [[NSMutableArray alloc] init];
    NSData * theData =[NSData dataWithContentsOfFile:path];
    
    if([theData length] > 0) {
        NSKeyedUnarchiver * archiver = [[NSKeyedUnarchiver alloc]
                                        initForReadingWithData:theData];
        listData = [archiver decodeObjectForKey:ARCHIVE_KEY];
        [archiver finishDecoding];
        
        for (Note* note in listData) {
            
            //比较日期主键是否相等
            if ([note.date isEqualToDate:model.date]){
                return note;
            }
        }
    }
    return nil;
}

 

 

 

 

iOS 开发指南 第11章 数据持久化之对象归档 学习

标签:

原文地址:http://www.cnblogs.com/haugezi/p/4821124.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!