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
  • 相关阅读:
    actionscript项目中自加载preloader实现
    as3 弹性跑道模型
    快速排序
    快捷键
    frameset和iframe--框架对象及元素标签对象
    Javascript的陷阱
    JS火狐与IE的差别
    JS运动框架
    面向对象与闭包的使用
    闭包
  • 原文地址:https://www.cnblogs.com/cqchen/p/3793852.html
Copyright © 2011-2022 走看看