OS开发指南读书笔记9(IOS数据持久层的建立1)

IOS开发指南读书笔记9(IOS数据持久层的建立1)
  建立IOS数据持久层,有以下几个方式
1、文本文件 plist/Xml/Json/NSUserDefault(等同于plist)
2、对像归档,反归档
3、SQLite数据库
4、Core Data


1、Xml(略)、NSUserDefault(略)\Json略
plist实现

plist 根节点为NSArray/NSDictionary

NSArray的主要方法
+arrayWithContentOfFile。
-initWithContentOfFile。
-writeToFile:atomically.

 NSDictionary的主要方法
+dictionaryWithContentOfFile.
-initWithContentOfFile。
-writeToFile:atomically。

假设我们建立一个Note为我们的数据模型
 
@interface  Note :  NSObject
 
//ID
 
@property ( nonatomic , assign NSInteger  ID;
 
// 内容
 
@property ( nonatomic , copy NSString * content;
 
// 根据字典初始化
 
-( Note *)initWithDictionary:( NSDictionary *)dictionary;
 
// 转换成字典
 
-( NSDictionary *)dictionaryValue;
 
  @end 

 然后建立一个基本数据管理协议BaseNoteDao

@protocol BaseNoteDao <NSObject>
@required
 
//单例构造方法
 
+(id)sharedManager;
 
//文件路径
 
-(id)ObjectCacheFilePath;
 
//创建文件
 
-(void)createObjectCacheFile;
 
//添加笔记
 
-(void)addNote:(Note*)note;
 
//删除笔记
 
-(void)removeNote:(Note*)note;
 
//修改笔记
 
-(void)modify:(Note*)note;
 
//查询所有记录
 
-(NSMutableArray*)queryAllNote;
 
//查询指定记录
 
-(Note*)queryNoteByID:(NSInteger)ID;
 
@end

然后建立一个PlistNoteDao plist数据管理

//plist数据管理
 
@interface PlistNoteDao : NSObject<BaseNoteDao>
 
@end


具体实现方式 

 
@implementation PlistNoteDao


+(id)sharedManager

{

    static dispatch_once_t onceToken;

    __block PlistNoteDao* instance = nil;

    dispatch_once(&onceToken, ^{

        instance = [[PlistNoteDao alloc]init];

        [instance createObjectCacheFile];

    });

    return instance;

}


-(void)createObjectCacheFile

{

    NSFileManager* fileManager = [NSFileManager defaultManager];

    NSString* objectDataFilePath = [self ObjectCacheFilePath];

    BOOL exist = [fileManager fileExistsAtPath:objectDataFilePath];

    if (!exist) {

        NSString* defaultPath = [[NSBundle mainBundlepathForResource:@"Notes" ofType:@"plist"];

        NSError* error;

        BOOL success = [fileManager copyItemAtPath:defaultPath toPath:objectDataFilePath error:&error];

        if (!success) {

            NSAssert1(0@"写入错误,错误信息:%@", [error localizedDescription]);

        }

    }

}


-(NSString *)ObjectCacheFilePath

{

    NSString* documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectoryNSUserDomainMaskYESlastObject];

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

}


-(void)addNote:(Note *)note

{

    NSString* objectDataFilePath = [self ObjectCacheFilePath];

    NSMutableArray* datas = [[NSMutableArray alloc]initWithContentsOfFile:objectDataFilePath];

    [datas addObject:[note dictionaryValue]];

    [datas writeToFile:objectDataFilePath atomically:YES];

}


-(void)removeNote:(Note *)note

{

    NSString* objectDataFilePath = [self ObjectCacheFilePath];

    NSMutableArray* datas = [[NSMutableArray alloc]initWithContentsOfFile:objectDataFilePath];

    for (NSDictionary* dict in datas) {

        if ([dict[@"ID"integerValue] == note.ID) {

            [datas removeObject:dict];

            [datas writeToFile:objectDataFilePath atomically:YES];

            break;

        }

    }

}


-(void)modify:(Note *)note

{

    NSString* objectDataFilePath = [self ObjectCacheFilePath];

    NSMutableArray* datas = [[NSMutableArray alloc]initWithContentsOfFile:objectDataFilePath];

    for (NSDictionary* dict in datas) {

        if ([dict[@"ID"integerValue] == note.ID) {

            [dict setValuesForKeysWithDictionary:[note dictionaryValue]];

            [datas writeToFile:objectDataFilePath atomically:YES];

            break;

        }

    }

}


-(NSMutableArray*)queryAllNote

{

    NSString* objectDataFilePath = [self ObjectCacheFilePath];

    NSMutableArray* datas = [[NSMutableArray alloc]initWithContentsOfFile:objectDataFilePath];

    NSMutableArray* notes = [NSMutableArray array];

    for (NSDictionary* dict in datas) {

        Note* note = [[Note alloc]initWithDictionary:dict];

        [notes addObject:note];

    }

    return notes;

}


-(Note*)queryNoteByID:(NSInteger)ID

{

    NSString* objectDataFilePath = [self ObjectCacheFilePath];

    NSMutableArray* datas = [[NSMutableArray alloc]initWithContentsOfFile:objectDataFilePath];

    for (NSDictionary* dict in datas) {

        if ([dict[@"ID"integerValue] == ID) {

            return [[Note alloc]initWithDictionary:dict];

        }

    }

    return nil;

}


@end


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值