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 mainBundle] pathForResource:@"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(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
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