您的位置:首页 > 数据库

Plist 数据库的使用方法

2015-11-11 22:07 330 查看
plist一,简单介绍一下常用的plist文件。全名是:Property List,属性列表文件,它是一种用来存储串行化后的对象的文件。属性列表文件的扩展名为.plist ,因此通常被称为 plist文件。文件是xml格式的。
Plist文件通常用于储存用户设置,也可以用于存储捆绑的信息 。NSUserDefault 轻量级数据持久化工具 NSUserDefault本质是一个本地的plist文件。只能接收基础数据类型的数据进行存储二,先说文件保存到的几个位置:1,工程沙盒里(就是程序user Document文件夹下,以读取文件,写入文件方式)2,工程自身里(就是在工程里手动创建一个如.plist文件,把固定的内容写入,这个需要人工手动写入)3,工程沙盒里(保存到user Document下,不过不需要读写文件,用系统的 NSUserDefaults 可以快速保存添加读取删除基本数据类型,类似于android里的Sharedpreferences )先封装一个 类//获取 一个文件 在沙盒Library/Caches/ 目录下的路径+ (NSString *)getFullPathWithFile:(NSString *)urlName {//先获取 沙盒中的Library/Caches/路径NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];NSString *myCacheDirectory = [docPath stringByAppendingPathComponent:@"MyCaches"];//检测MyCaches 文件夹是否存在if (![[NSFileManager defaultManager] fileExistsAtPath:myCacheDirectory]) {//不存在 那么创建[[NSFileManager defaultManager] createDirectoryAtPath:myCacheDirectory withIntermediateDirectories:YES attributes:nil error:nil];}////用md5进行 加密 转化为 一串十六进制数字 (md5加密可以把一个字符串转化为一串唯一的用十六进制表示的串)NSString * newName = [urlName MD5Hash];//拼接路径return [myCacheDirectory stringByAppendingPathComponent:newName];}//导入头文件#import <CommonCrypto/CommonDigest.h>- (NSString *)MD5Hash{const char *cStr = [self UTF8String];unsigned char result[16];CC_MD5(cStr, strlen(cStr), result);return [NSString stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",result[0], result[1], result[2], result[3],result[4], result[5], result[6], result[7],result[8], result[9], result[10], result[11],result[12], result[13], result[14], result[15]];}NSString *path = [self getFullPathWithFile:url];//读本地NSData *data = [NSData dataWithContentsOfFile:path];//写本地[data writeToFile:path atomically:YES];//图片写入本地沙河的方法[UIImageJPEGRepresentation(imagV.image, 1) writeToFile:path atomically:YES];/由于图片存入的是时候使用加密的方式 因此取出的时候要//取得一个目录下得所有文件名NSArray *filesArr = [[NSFileManager defaultManager] subpathsAtPath: myCacheDirectory];NSLog(@"%lu",(unsigned long)filesArr.count);for (NSInteger i = 0; i<filesArr.count; i++) {NSString *filePath = [myCacheDirectory stringByAppendingPathComponent:filesArr[i]];UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfFile:filePath]];// NSData *data = [NSData dataWithContentsOfFile:arr[i]];//UIImage *imag = [UIImage imageWithData:data];[self.dataArr addObject:image];}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: