美文网首页
数据持久化

数据持久化

作者: 少年不知代码贵 | 来源:发表于2016-12-01 18:54 被阅读0次

    //把数据存到硬盘

    //iOS, 沙盒机制

    //1.系统会为每个app, 分配一个文件夹(沙盒)

    //2.每个app, 只能访问自己的沙盒; 系统的相册, 视频, 短信, 通讯录, 是可以访问的, 前提: 需要申请权限

    //沙盒路径

    //1.沙盒主目录, 名字无规律, 能避免别的应用访问

    NSString *sandBoxPath = NSHomeDirectory();

    NSLog(@"%@", sandBoxPath);

    //1.

    NSString *documentPath = [sandBoxPath stringByAppendingString:@"/Documents"];

    //2.

    //NSString *documentsPath1 = [sandBoxPath stringByAppendingPathComponent:@"Documents"];

    //3.

    //NSString *documengsPath2 = [NSString stringWithFormat:@"%@/Documents", sandBoxPath];

    NSLog(@"%@", documentPath);

    //沙盒主目录里有三个文件夹

    //1.Documents: 存储一些比较重要的数据, 比如: 游戏存档, 用户信息等重要文件

    //注: Documents中的文件, 在同步到iCloud的时候会一并同步进去, 所以要求Docements中的文件不能过大, 否则app没办法通过审核, 进而无法在appStore上线

    //查找文件夹方法

    //参数1: 文件夹名称

    //参数2: 搜索的区域, 只在用户域(NSUserDomainMask)

    //参数3: 返回的路径 是 相对路径no 还是 绝对路径yes, 使用绝对路径

    NSString *documentsPath3 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];

    NSLog(@"%@", documentsPath3);

    //2.Library, 存放视频, 图片, 音频, 缓存, 配置等文件

    NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject];

    NSLog(@"%@", libraryPath);

    //Library. 有两个子文件夹

    //a.Caches, 缓存文件

    NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];

    NSLog(@"%@", cachesPath);

    //b.Preferences, 配置文件

    //没有提供查找路径的方法, 不会直接在文件夹中对文件进行修改, 而是是Xcode中进行修改, 系统会自动同步到Preferences中去

    //3.tmp, 临时文件文件, 比如zip压缩包

    NSString *tmpPath = NSTemporaryDirectory();

    NSLog(@"%@", tmpPath);

    //4.包, 程序编译后, 最终形成一个包(比如*.app), 包内的文件是只读, 不能修改; Xcode中添加的文件, 比如图片, 文档等, 最终都会放到包中

    NSString *bundlePath = [[NSBundle mainBundle] bundlePath];

    NSLog(@"%@", bundlePath);

    //数据持久化方式(这个是存到配置文件tmp中)

    //1.文件读写

    //2.NSUserDefaults

    //3.归档

    //4.数据库

    //5.CoreData

    //读取数据的方法

    //documents文件路径

    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];

    //文件路径

    NSString *filePath = [documentsPath stringByAppendingPathComponent:@"money.txt"];

    NSError *error = nil;

    NSString *moneyString = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];

    if (error) {

    NSLog(@"读取失败:%@", error);

    }else{

    NSLog(@"读取成功");

    self.money = [moneyString integerValue];

    self.label.text = [NSString stringWithFormat:@"%ld", self.money];

    }

    //1. 文件读写

    //适用范围: 简单地数据, NSString, NSArray, NSDictionary, NSData以及他们的子类

    //NSString

    //写入文件

    //- WriteToFile:atomically:encoding:error:

    //读取文件

    //initWithContentsOfFile: encoding: error:

    //NSArray

    //写入文件

    //writeToFile: atomically:

    //读取文件

    //initWithContentsOfFile:(也有便利构造器)

    NSArray *array = @[@"校花", @"校草", @"笑话"];

    //写入文件

    NSString *libraryPath1 = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject];

    NSString *filePath1 = [libraryPath1 stringByAppendingPathComponent:@"array.txt"];

    BOOL result = [array writeToFile:filePath1 atomically:YES];

    if (result) {

    NSLog(@"写入成功");

    }else{

    NSLog(@"写入失败");

    }

    //读取文件内容

    NSArray *array1 = [[NSArray alloc] initWithContentsOfFile:filePath1];

    //NSArray *array1 = [NSArray arrayWithContentsOfFile:filePath1];

    for (NSString *string in array1) {

    NSLog(@"%@", string);

    }

    [array1 release];

    //NSDictionary

    //写入文件

    NSDictionary *dic = @{@"name" : @"校花", @"age" : @"18", @"class" : @"24"};

    //写入文件

    NSString *cachesPath1 = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];

    NSString *filePath2 = [cachesPath1 stringByAppendingPathComponent:@"girl.plist"];

    BOOL result2 = [dic writeToFile:filePath2 atomically:YES];

    if (result2) {

    NSLog(@"写入成功");

    }else{

    NSLog(@"写入失败");

    }

    //注: Array 和 Dictionary的数据, 建议存的文件使用后缀plist, 这样看起来比较直观

    NSDictionary *dic2 = [[NSDictionary alloc] initWithContentsOfFile:filePath2];

    NSLog(@"%@", dic2);

    [dic2 release];

    //NSDictionary

    //写入文件

    //writeToFile: atomically:

    //读取文件

    //initWithContentsOfFile:(也有便利构造器)

    //NSData

    //写入文件

    //writeToFile: atomically:

    //读取文件

    //initWithContentsOfFile:(也有便利构造器)

    NSString *str = @"iOS";

    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];

    NSString *tmpPath3 = NSTemporaryDirectory();

    NSString *filePath3 = [tmpPath3 stringByAppendingPathComponent:@"data.txt"];

    BOOL result3 = [data writeToFile:filePath3 atomically:YES];

    if (result3) {

    NSLog(@"写入成功");

    }else{

    NSLog(@"写入失败");

    }

    NSData *data3 = [[NSData alloc] initWithContentsOfFile:filePath3];

    NSString *string3 = [[NSString alloc] initWithData:data3 encoding:NSUTF8StringEncoding];

    NSLog(@"%@", string3);

    //NSUserDefaults, 系统封装的一个快速写入内容的一个类, 单例模式, 继承于NSObject

    //使用范围: BOOL, float, double, NSInteger

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

    //写入文件

    //BOOL

    [userDefaults setBool:YES forKey:@"isFirst"];

    [userDefaults setInteger:100 forKey:@"age"];

    [userDefaults setFloat:99.9 forKey:@"price"];

    [userDefaults setDouble:10.00 forKey:@"weight"];

    [userDefaults setObject:@"zhangsan" forKey:@"name"];

    //同步

    [userDefaults synchronize];

    //读取内容

    BOOL boolValue = [userDefaults boolForKey:@"isFirst"];

    NSLog(@"%d", boolValue);

    NSInteger age = [userDefaults integerForKey:@"age"];

    NSLog(@"%ld", age);

    CGFloat weight =[userDefaults doubleForKey:@"weight"];

    NSLog(@"%.2lf", weight);

    CGFloat price = [userDefaults floatForKey:@"price"];

    NSLog(@"%.2lf", price);

    NSString *name =[userDefaults objectForKey:@"name"];

    NSLog(@"%@", name);

    //3.归档

    //适用范围: 主要用于存自定义的数据类型, 比如Person

    Person *person = [[Person alloc] init];

    person.name = @"校花";

    person.age = @"18";

    person.gender = @"女";

    //NSKeyedArchiver, 压缩工具, 继承于NSCoder, 把一个对象进行编码, 转化成NSData

    NSMutableData *mData = [[NSMutableData alloc] initWithCapacity:0];

    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:mData];

    //把person对象压进data

    [archiver encodeObject:person forKey:@"girlFriend"];

    //完成压缩

    [archiver finishEncoding];

    //tmp, 临时文件文件, 比如zip压缩包(写入文件(自定义一个路径))

    NSString *tmpPath1 = NSTemporaryDirectory();

    NSString *filePath5 = [tmpPath1 stringByAppendingPathComponent:@"person.txt"];

    BOOL result5 = [mData writeToFile:filePath5 atomically:YES];

    if (result5) {

    NSLog(@"写入成功");

    }else{

    NSLog(@"写入失败");

    }

    //反归档

    NSData *mData1 = [[NSData alloc] initWithContentsOfFile:filePath5];

    //NSKeyedUnarchiver, 解压工具, 从data数据提取对象

    NSKeyedUnarchiver *unArchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:mData1];

    Person *person1 = [unArchiver decodeObjectForKey:@"girlFriend"];

    NSLog(@"%@", person1);

    //归档 与 反归档

    //多用于自定义数据类型, 比如Person, 并且能够进行归档的数据类型, 必须遵守, 实现编码和解码的方法, 对属性进行编码和解码, 编码和解码的key值必须相同

    }

    相关文章

      网友评论

          本文标题:数据持久化

          本文链接:https://www.haomeiwen.com/subject/xnxfmttx.html