IOS--UI--LessonDataPersistiser 数据持久化

//沙盒和包
//沙盒是系统为每一个应用程序生成的一个特定文件夹文件夹的名字由 十六进制数据组成,每一应用程序的沙盒文件名都是不一样的,是由系统随机生成的;

    //沙盒主目录
    NSString *homePath = NSHomeDirectory();
    NSLog(@"%@",homePath);

//Documents 存放是一些比较重要的文件,但是放入Documents中的文件不能过大;
//如何获取Documents文件目录
// NSSearchPathDirectory这个类使用来查找文件目录的
//第一个参数:文件名称
//第二个参数:确定你的搜索域
//第三个参数:确定是相对路还是绝对路径,YES 绝对 NO 相对
//该方法一开始使用MAC端OS X开发,对于PC端用户可以有多个用户,所以可以取到很多user的路径,但是该方法现在用于手机端开发,而手机端只有一个用户,所以获得的用户只有一个;

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

    NSLog(@"%@",documentsPath);

//Library :是一个资源库,存储一些不太重要的数据,相对比较大一些,里面有两个子文件夹;

    NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)firstObject];
    NSLog(@"%@",libraryPath);
//Caches:缓存文件,图片缓存,小电影缓存,网页缓存,应用程序清除缓存就是清楚这个文件夹
  NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)firstObject];
    NSLog(@"%@",cachesPath);
//perferences 系统偏好设置,用户对应用程序的设置,比如:用户名和用户密码
//perferences路径无法找不到,通过NSUserDefaults

//tmp,存放一些临时文件,比如下载压缩包Zip,解压后立即把压缩包删除
   NSString *tmpPath =  NSTemporaryDirectory();
    NSLog(@"%@",tmpPath);
//NSBundle.包文件
//查看.app文件,右键显示包内容,查看里面存法的文件
//iOS8之前包和沙盒在同一个目录下,iOS8之后.app单独存储到一个独立的文件目录下
//.app文件只能读不能写(readOnly),从AppStore下载下来的是这个包,程序上传的时候也是这个包;
    NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
    NSLog(@"%@",bundlePath);

//NSFileManger
//NSFileManager 文件管理工具,重要用于添加 删除 移动 拷贝等,继承自NSObject
//1.在Caches文件下创建一个Image文件

 //获取Caches文件夹路径
    NSString *cachesPath =   [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)firstObject];
//拼接Image文件夹路径
//stringByAppendingFormat 拼接上什么就是什么;
//stringByAppendingPathExtension 会在拼接内容前加一个 .
 //stringByAppendingString 拼接上什么就是什么
   NSString *imagePath = [cachesPath stringByAppendingPathComponent:@"imagessss"]
    ;
    NSLog(@"%@",imagePath);

//NSFileManager是一个单例
//创建文件管理者
NSFileManager *manager = [NSFileManager defaultManager];

//判断一个文件夹是否存在
BOOL isExist = [manager fileExistsAtPath:imagePath];
if (!isExist) {
    //不存在
   BOOL isSucess = [manager createDirectoryAtPath:imagePath withIntermediateDirectories:YES attributes:nil error:nil];
    NSLog(@"%@",isSucess ? @"创建成功" : @"创建失败");
}
//文件删除  这个地方 你之后要记得注掉 否则 后面的操作 都会返回失败 因为你把文件删掉了

{
//删除文件
if ([manager fileExistsAtPath:imagePath]) {

  BOOL isSucess = [manager removeItemAtPath:imagePath error:nil];
  NSLog(@"%@",isSucess ? @"删除成功" : @"删除失败");

}
}

//拷贝文件
//需求:把包中的plist文件,拷贝到Image文件夹下

 //1.先获取到plist文件的路径
    NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"NB" ofType:@"plist"];

 //2.获取拷贝需要的文件路径,其实就是拿到Image在沙盒里的路径
    NSString * nBPath =  [imagePath stringByAppendingPathComponent:@"NB.plist"];

 //3.完成拷贝工作 判断一下 有没有这个东西
    if (![manager fileExistsAtPath:nBPath]) {
            //不存在文件路径的时候拷贝
      BOOL isSuccess =   [manager copyItemAtPath:bundlePath toPath:nBPath error:nil];
        NSLog(@"%@",isSuccess ? @"拷贝成功" : @"拷贝失败");

    }

移动文件
//1.移动之前的路径 nBPath
//2.移动之后的路径
//2.1.获取Documents路径,2.2.拼接@”NB.plist”

   NSString *toPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"NB.plist"];

    //移动
   BOOL isSuccess =  [manager moveItemAtPath:nBPath toPath:toPath error:nil];
    NSLog(@"%@",isSuccess ? @"移动成功" : @"移动失败");
 }

//NSUserDefaults的数据持久化

//NSUserDefaults 继承自NSObject,也是一个单例,通过KVC模式去赋值
//存取数据时所操作的内容,都存放在perference,
NSLog(@"%@",NSHomeDirectory());
 //创建用户索引对象
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setInteger:100 forKey:@"money"];
    [defaults synchronize]; //立即同步操作

    NSInteger money = [defaults integerForKey:@"money"];
    NSLog(@"%ld",money);

    [defaults setInteger:10000 forKey:@"MyMoney"];
    [defaults synchronize];//只要修改了perferences文件中plist文件,就要立即同步
    NSInteger yMoney = [defaults integerForKey:@"YouMoney"];
    NSLog(@"%ld",yMoney);

    if (money < 10) {
        NSLog(@"钱不够花了");
    }else{
        NSLog(@"花了60元,大保健半小时");
     //        money -= 60;
        //要想修改plist文件中的值必须通过KVC的形式修改
        [defaults setInteger:40 forKey:@"money"];
        [defaults setInteger:1000 forKey:@"YouMoney"];
        [defaults synchronize];
    }

//NSUserDefault还能操作其他数据类型:NSDictionary ,NSArray,NSString,NSData,NSNumber,BOOL,NSInteger
//NSUserDefaults 一般存储一些比较小的数据,大部分都用来存数值

  NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    BOOL isFirst = [userDefault boolForKey:@"isFirst"];
    if (isFirst == NO ) {
        NSLog(@"第一次登录");
        [userDefault setBool:YES forKey:@"isFirst"];
        [userDefault synchronize];
    }else{
        NSLog(@"不是第一次登录");
    }
    }

注意:
//简单对象写入文件:字符串,数组,字典,二进制流(NSData)
//只有这些简单对象支持文件的写入
//如果你要写入文件的数据是(数组,字典),你必须要保证(数组,字典)中存入的数据也是这四种简单之一;
//字符串

 //1.字符串写入文件
    //将字符串写入Documents文件夹下
    //获取Docunment文件路径
    NSString *docmentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
    //string.txt就是我们字符串要写入的文件
    NSString *stringPath = [docmentsPath stringByAppendingPathComponent:@"string.txt"];
    //准要写入的字符串
    NSString *string = @"马震和优衣库你选哪一个?";
    //第一个参数:要写入的文件路径,如果文件路径下没有此文件,系统会自动创建一个文件
    //第二个参数:判断是否需要生成辅助文件,为了保护在多线程下的安全
    //第三参数:编码格式
    //第四个参数:错误
    NSError *error = nil;
   BOOL isSuccess = [string writeToFile:stringPath atomically:YES encoding:NSUTF8StringEncoding error:&error];
    NSLog(@"%@",isSuccess ? @"字符串写入成功" : @"字符串写入失败");
 //字符串取值操作
    NSString *getString = [[NSString alloc]initWithContentsOfFile:stringPath encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"%@",getString);
    } 

//数组

  //NSArray的文件写入
    //获取library路径
    NSString *librarayPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)firstObject];
    //将array.txt拼接到libraryPath后
    NSString *arrayPath = [librarayPath stringByAppendingPathComponent:@"array.txt"];

    //准备写入文件的数组
    NSArray *array = @[@"龙庆",@"小芳",@"张越",@"二甫"];

    //写入
    //第一个参数:文件路径
    //第二参数:多线程保护
    BOOL isSuccess = [array writeToFile:arrayPath atomically:YES];
    NSLog(@"%@",isSuccess ? @"数组写入成功" : @"数组写入失败");
    //数组读取
    NSArray *getArray = [NSArray arrayWithContentsOfFile:arrayPath];
    NSLog(@"%@",getArray);
     }

//字典

    {
    //字典写入文件
    NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)firstObject];

    //dictioary.txt
    NSString * dictionaryPath = [cachesPath stringByAppendingPathComponent:@"dictionary.txt"];
    //准备写入的字典
    NSDictionary *dictionary = @{@"a":@"123",@"b":@"456",@"c":@"789"};
    //写入
    BOOL isSuccess =  [dictionary writeToFile:dictionaryPath atomically:YES];
    NSLog(@"%@",isSuccess ? @"字典写入成功" : @"字典写入失败");

    //从文件中读取
    NSDictionary *getDictionary = [NSDictionary dictionaryWithContentsOfFile:dictionaryPath];
    NSLog(@"%@",getDictionary);
    }

// NSData(二进制流)文件写入

   NSString *tmpPath = NSTemporaryDirectory();

    //data.txt拼接到tmpPath后
    NSString *dataPath = [tmpPath stringByAppendingPathComponent:@"data.txt"];

    //准备要写入的NDData对象
    NSString *string = @"叔叔,咱们约吧!";
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];

    //写入
    BOOL isSuccess = [data writeToFile:dataPath atomically:YES];
    NSLog(@"%@",isSuccess ? @"Data写入成功" : @"Data写入失败");
    //data从文件中取值
    NSData *getData = [NSData dataWithContentsOfFile:dataPath];
    NSLog(@"%@",getData);
    NSString *getString = [[NSString alloc]initWithData:getData encoding:NSUTF8StringEncoding];
    NSLog(@"%@",getString);
    }

warning mark ——-VVIP——

//复杂对象的归档和反归档
//复杂对象写入文件
//VVIP 能进行归档和反归档的复杂对象必须遵循NSCoding协议
//复杂对象是无法写入文件的,要想把复杂对象写入文件,必须要把他转换成data(二进制流)才能写入;
//归档:归档的实质就是把其他类型数据(person),先转化成NSData,再写入文件
//创建复杂对象person

  Person *person = [[Person alloc]initWithName:@"rose" gender:@"girl" age:18];

//   NSKeyedArchiver 压缩工具,继承自NSCoder,主要用于编码
    //创建可变data永存压缩后的数据
    NSMutableData *data= [NSMutableData data];
    NSKeyedArchiver *archiver =[[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
    //使用压缩工具将person对象压到data
    [archiver encodeObject:person forKey:@"person"];

    //完成压缩,要停掉压缩工具
    [archiver finishEncoding];
    [person release];
    [archiver release];

    NSString *homePath = NSHomeDirectory();
    //将压缩后的人方法person.txt文件
    NSString *personPath = [homePath stringByAppendingPathComponent:@"perosn.txt"];
    BOOL isSuccess = [data writeToFile:personPath atomically:YES];
    NSLog(@"%@",isSuccess ? @"person 写入成功" : @"person 写入失败");

//复杂对象的读取叫做反归档

//    NSKeyedUnarchiver 反归档工具

    //获取文件中数据,(1)文件路径,(2)找一data对象接受读取结果
    NSData *unData = [NSData dataWithContentsOfFile:personPath];
    //使用反归档工具
    NSKeyedUnarchiver *unArchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:unData];
    //解压
    Person *unPerson = [unArchiver decodeObjectForKey:@"person"];
//快捷键
    //option + command + {(上移) }(下移)
    //command + {(左移) }(右移)
    //停止解压
    [unArchiver finishDecoding];
    [unArchiver release];
    NSLog(@"%@",unPerson);
}

② //数组归档和反归档



    //NSArray
    //集合(NSArray, NSDictionary)如果想进行归档和返归档,那么它里面存储的元素必须也要遵循NSCoding协议
    Person *person1 = [[Person alloc]initWithName:@"范冰冰" gender:@"男" age:30];
    Person *person2 = [[Person alloc]initWithName:@"大黑牛" gender:@"女" age:28];
    NSArray *array = @[person1,person2];
    [person1 release];
    [person2 release];
    //创建压缩数据要存入的data对象
    NSMutableData *data = [NSMutableData data];
    //归档工具的创建
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
    //压缩归档的对象
    [archiver encodeObject:array forKey:@"array"];
    //压缩结束
    [archiver finishEncoding];
    [archiver release];
    //将文件存放到tmp目录下,array.txt
    NSString *tmpPath = NSTemporaryDirectory();
    NSString *arrayPath = [tmpPath stringByAppendingPathComponent:@"array.txt"];
    BOOL isSuccess = [data writeToFile:arrayPath atomically:YES];
    NSLog(@"%@",isSuccess ? @"数组写入成功" : @"数组写入失败");
    //通过文件路径初始化unData
    NSData *unData = [NSData dataWithContentsOfFile:arrayPath];
    //创建方归档对象
    NSKeyedUnarchiver *unArchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:unData];
    //解压数据
    NSArray *unArray = [unArchiver decodeObjectForKey:@"array"];
    //停止反归档工具
    [unArchiver finishDecoding];
    [unArchiver release];
    NSLog(@"%@ %@",unArray[0],unArray[1]);





}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值