IOS开发指南读书笔记10(IOS数据持久层的建立2)

本文介绍了一种基于对象归档的iOS数据持久化方法。通过实现NSCoding协议,使用NSKeyedArchiver和NSKeyedUnarchiver进行数据的存取。文章详细展示了如何创建归档管理类以及实现数据的添加、删除、修改和查询操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

IOS开发指南读书笔记10(IOS数据持久层的建立2)
基于对象归档的实现
建立对象归档数据管理类

实现归档数据持久的对象实现NSCoding协议
其属性也必须是基本类型或者实现NSCoding协议的对象
我们堆Note做如下更改添加这两个方法

-(void)encodeWithCoder:(NSCoder *)aCoder

{

    //编码

    [aCoder encodeObject:[NSNumber numberWithInteger:_IDforKey:@"ID"];

    [aCoder encodeObject:_content forKey:@"content"];

}


-(id)initWithCoder:(NSCoder *)aDecoder

{

    self = [super init];

    if (self) {

        //解码

        self.ID = [aDecoder decodeIntegerForKey:@"ID"];

        self.content = [aDecoder decodeObjectForKey:@"content"];

    }

    return self;

}



//归档数据管理
@interface ArchiveNoteDao : NSObject<BaseNoteDao>
 
#define ARCHIVE_KEY @"Notes"
 
@ end

具体实现

@implementation ArchiveNoteDao

+(id)sharedManager

{

    static dispatch_once_t onceToken;

    __block ArchiveNoteDao* instance = nil;

    dispatch_once(&onceToken, ^{

        instance = [[ArchiveNoteDao alloc]init];

        [instance createObjectCacheFile];

    });

    return instance;

}


-(void)createObjectCacheFile

{

    NSFileManager* fileManager = [NSFileManager defaultManager];

    NSString* objectDataFilePath = [self ObjectCacheFilePath];

    BOOL exist = [fileManager fileExistsAtPath:objectDataFilePath];

    if (!exist) {

        //添加一条伪数据

        Note* note = [[Note alloc]init];

        note.ID = 0;

        note.content = @"今天天气晴转阴";

        NSMutableArray* array = [@[note] mutableCopy];

        //保存数组

        [self saveWtihArray:array];

    }

}


//根据数组保存到归档文件

-(void)saveWtihArray:(NSMutableArray*)array

{

    NSString* objectDataFilePath = [self ObjectCacheFilePath];

    NSMutableData* theData = [NSMutableData data];

    NSKeyedArchiver* archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:theData];

    [archiver encodeObject:array forKey:ARCHIVE_KEY];

    [archiver finishEncoding];

    [theData writeToFile:objectDataFilePath atomically:YES];

}


-(NSString *)ObjectCacheFilePath

{

    NSString* documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectoryNSUserDomainMaskYESlastObject];

    return [documentDirectory stringByAppendingPathComponent:@"Notes.archive"];

}


-(void)addNote:(Note *)note

{

    NSMutableArray* datas = [self queryAllNote];

    [datas addObject:note];

    [self saveWtihArray:datas];

}


-(void)removeNote:(Note *)note

{

    NSMutableArray* datas = [self queryAllNote];

    for (Note* sNote in datas) {

        if (sNote.ID == note.ID) {

            [datas removeObject:sNote];

            [self saveWtihArray:datas];

            break;

        }

    }

}


-(void)modify:(Note *)note

{

    NSMutableArray* datas = [self queryAllNote];

    for (Note* sNote in datas) {

        if (sNote.ID == note.ID) {

            [sNote modifyWithOther:note];

            [self saveWtihArray:datas];

            break;

        }

    }

}


-(NSMutableArray*)queryAllNote

{

    NSString* objectDataFilePath = [self ObjectCacheFilePath];

    NSMutableArray* datas = [[NSMutableArray alloc]init];

    NSData* archiveData = [NSData dataWithContentsOfFile:objectDataFilePath];

    if ([archiveData length] > 0) {

        //反归档

        NSKeyedUnarchiver* unArchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:archiveData];

        datas =  [unArchiver decodeObjectForKey:ARCHIVE_KEY];

        [unArchiver finishDecoding];

    }

    return datas;

}


-(Note*)queryNoteByID:(NSInteger)ID

{

    NSMutableArray* datas = [self queryAllNote];

    for (Note* sNote in datas) {

        if (sNote.ID == ID) {

            return sNote;

        }

    }

    return nil;

}


@end 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值