#import <Foundation/Foundation.h> #import "Persistence01Note.h" @interface Persistence01NoteDAO : NSObject #pragma mark 单例 +(Persistence01NoteDAO *)sharedManager; #pragma mark NotesList.plist文件路径 -(NSString *)applicationDocumentsDirectoryFile; #pragma mark 创建NotesList.plist文件 -(void)createEditableCopyOfDatabaseIfNeeded; #pragma mark 操作NotesList.plist文件 -(int)create:(Persistence01Note *)model; -(int)remove:(Persistence01Note *)model; -(int)modify:(Persistence01Note *)model; -(NSMutableArray *)findAll; -(Persistence01Note *)findById:(Persistence01Note *)model; @end
#import "Persistence01NoteDAO.h" @implementation Persistence01NoteDAO static Persistence01NoteDAO *sharedManager = nil; +(Persistence01NoteDAO *)sharedManager { static dispatch_once_t once; dispatch_once(&once, ^{ sharedManager = [[self alloc] init]; [sharedManager createEditableCopyOfDatabaseIfNeeded]; }); return sharedManager; } -(NSString *)applicationDocumentsDirectoryFile { NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString *path = [documentDirectory stringByAppendingPathComponent:@"NotesList.plist"]; return path; } -(void)createEditableCopyOfDatabaseIfNeeded { NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *writableDBPath = [self applicationDocumentsDirectoryFile]; BOOL dbexists = [fileManager fileExistsAtPath:writableDBPath]; if (!dbexists) { NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"NotesList.plist"]; NSError *error; BOOL success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error]; if (!success) { NSAssert(0, @"错误写入文件:'%@'。",[error localizedDescription]); } } } -(int)create:(Persistence01Note *)model { NSString *path = [self applicationDocumentsDirectoryFile]; NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:path]; NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSDictionary *dict = [NSDictionary dictionaryWithObjects:@[[dateFormat stringFromDate:model.date],model.content] forKeys:@[@"date",@"content"]]; [array addObject:dict]; [array writeToFile:path atomically:YES]; return 1; } -(int)remove:(Persistence01Note *)model { NSString *path= [self applicationDocumentsDirectoryFile]; NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:path]; for (NSDictionary *dict in array) { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSDate *date = [dateFormatter dateFromString:[dict objectForKey:@"date"]]; // 比较日期主键是否相等 if ([date isEqualToDate:model.date]) { [array removeObject:dict]; [array writeToFile:path atomically:YES]; break; } } return 1; } -(int)modify:(Persistence01Note *)model { NSString *path = [self applicationDocumentsDirectoryFile]; NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:path]; for (NSDictionary *dict in array) { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSDate *date = [dateFormatter dateFromString:[dict objectForKey:@"date"]]; NSString *content = [dict objectForKey:@"content"]; // 比较日期主键是否相等 if ([date isEqualToDate:model.date]) { [dict setValue:content forKey:@"content"]; [array writeToFile:path atomically:YES]; break; } } return 1; } -(NSMutableArray *)findAll { NSString *path = [self applicationDocumentsDirectoryFile]; NSMutableArray *listData = [[NSMutableArray alloc] init]; NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:path]; for (NSDictionary *dict in array) { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; Persistence01Note *note = [[Persistence01Note alloc] init]; note.date = [dateFormatter dateFromString:[dict objectForKey:@"date"]]; note.content = [dict objectForKey:@"content"]; [listData addObject:note]; } return listData; } -(Persistence01Note *)findById:(Persistence01Note *)model { NSString *path = [self applicationDocumentsDirectoryFile]; NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:path]; for (NSDictionary *dict in array) { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; Persistence01Note *note = [[Persistence01Note alloc] init]; note.date =[dateFormatter dateFromString:[dict objectForKey:@"date"]]; note.content = [dict objectForKey:@"content"]; if ([note.date isEqualToDate:model.date]) { return note; } } return nil; } @end