1 概述
沙箱目录:一种安全策略,原理是只能允许自己的应用访问目录,而不许其他应用访问。
子目录:Documents 用于储存非常大的文件或需要非常频繁更新的数据
NSArray *documentDirectory=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
documentDirectory是只有一个元素的数组,还需要取出路径
NSString *myDocPath=[documentDirectory objectAtIndex:0];
Library Preferences用于存放应用程序的设置数据 Caches用于存放缓存文件
tmp 临时文件目录 NSString *temDirectory=NSTemporaryDirectory();
2 持久化方式 数据储存方式
属性列表:是XML文件,数组和字典可以与属性列表文件相互转换。
NSArray和NSDictionary提供了读写属性列表文件的方法:+array/dictionaryWithContentsOfFile:
-initWithContentOfFile:
writeToFile:atomically:是否使用辅助文件
获取文件代码:
- (NSString *)applicationDocumentsDirectoryFile {获取沙箱Documents目录文件的完整路径 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];获取文件目录 判断文件是否存在:fileExistsAtPath: BOOL dbexits = [fileManager fileExistsAtPath:writableDBPath]; if (!dbexits) {若不存在则复制一个 NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"NotesList.plist"]; NSError *error; BOOL success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error]; NSAssert(success, @"错误写入文件");断言函数,第一个参数为假时抛出异常 } }
读取与写入文件代码:程序的插入/删除/查询等无非是文件的操作
//插入Note方法 -(int) create:(Note*)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"]]; *setValue:forKey: * [array addObject:dict]; [array writeToFile:path atomically:YES]; return 0; } //删除Note方法 -(int) remove:(Note*)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"]; 是把value取出来而不是key比较 //Note* note = [[Note alloc] init]; NSDate *date = [dateFormatter dateFromString:[dict objectForKey:@"date"]]; //note.content = [dict objectForKey:@"content"]; //比较日期主键是否相等 if ([date isEqualToDate:model.date]){ [array removeObject: dict]; [array writeToFile:path atomically:YES]; break; } } return 0; } //修改Note方法 -(int) modify:(Note*)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 0; } //查询所有数据方法 -(NSMutableArray*) findAll { NSString *path = [self applicationDocumentsDirectoryFile]; //[self.listData removeAllObjects]; 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"]; Note* note = [[Note alloc] init]; note.date = [dateFormatter dateFromString:[dict objectForKey:@"date"]]; note.content = [dict objectForKey:@"content"]; [listData addObject:note]; } return listData; } //按照主键查询数据方法 -(Note*) findById:(Note*)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"]; Note* note = [[Note alloc] init]; note.date = [dateFormatter dateFromString:[dict objectForKey:@"date"]]; note.content = [dict objectForKey:@"content"]; //比较日期主键是否相等 日期字段是主键 if ([note.date isEqualToDate:model.date]){ return note; } } return nil; }
时间: 2024-08-02 07:00:08