zoukankan      html  css  js  c++  java
  • iOS.数据持久化.PersistenceLayer.属性列表

    #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
  • 相关阅读:
    mikadonic负载均衡RH6(band)RH7(team)
    Linux日志系统两个分类:rsyslog和journal
    Linux7 重置root密码
    mikadonicLINUX文件系统大小调整
    文件系统fdisk、gdisk、parted
    Linux文件权限设置
    使用python3从零开始写安全脚本(1)
    浅谈软件开发企业绩效管理中的问题与对策(四、绩效方案实例)
    非常道中小软件公司项目管理(一 项目管理终极目标)
    浅谈软件开发企业绩效管理中的问题与对策(二、管理理论篇)
  • 原文地址:https://www.cnblogs.com/cqchen/p/3793852.html
Copyright © 2011-2022 走看看