数据本地化之沙盒机制

1.什么是沙盒机制(SandBox)?每个iOS应用程序都会为自己创建一个文件系统目录(文件夹),这个独立,封闭,安全的空间,叫做沙盒.

注意:1.每一个应用程序都会拥有一个应用程序沙盒. 2.每一个程序沙盒就是一个文件系统目录.

2.沙盒的特点

3.沙盒的文件夹及各个文件夹的作用

 

4.简单数据类型写入本地(字符串,数组,字典,NSData类型的数据存储在本地)

#pragma mark - 简单对象的本地持久化
    
#pragma mark - 将NSString类型的数据存储到本地
    // 1. 需要知道这个对象存在哪里,所以需要一个文件夹的路径
    // 2. 找到Documents文件夹路径
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    // 3. 我们要知道存储什么?所以需要创建什么
    // 创建要存储的内容: 字符串
    NSString *str = @"liuaoran";
    // 4. 需要知道字符串最终存储的地方,所以需要创建一个路径去存储字符串
    NSString *strPath = [documentPath stringByAppendingPathComponent:@"leikun.txt"];
    
    // 5.准备工作完成,将字符串写入文件
    // 第一个参数: 写入的文件的路径
    // 第二个参数: 在断电的情况下,会不会自动保存
    // 第三个参数: 编码格式 NSUTF8StringEncoding
    // 第四个参数:
    [str writeToFile:strPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
    
    NSLog(@"strPath = %@", strPath);
    
#pragma mark - 将NSString文件夹存储的内容取出来
    // 将字符串取出
    //stringWithContentsOfFile这个方法将其取出
    // 第一个参数: 字符串存储的路径
    // 第二个参数: 编码格式
    // 第三个参数: 错误信息
    NSString *newStr = [NSString stringWithContentsOfFile:strPath encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"newStr = %@", newStr);
    
#pragma mark - 将NSArray类型的数据存储到本地
    // 1.找到对象存放的文件夹路径
    NSString *documentPath1 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    // 2.创建需要存储的数组
    NSArray *array = @[@"Black", @"MBBoy", @"seaBrother", @"BPY", @"BOOM"];
    // 3.创建数组存储最终路径
    NSString *arrayPath = [documentPath1 stringByAppendingPathComponent:@"leikun.plist"];
    // 4.将数组写入文件
    [array writeToFile:arrayPath atomically:YES];
    
    NSLog(@"arrayPath = %@", arrayPath);
    
    // 将存在本地的数组取出
    NSArray *newArray = [NSArray arrayWithContentsOfFile:arrayPath];
    NSLog(@"newArray = %@", newArray);
    
#pragma mark - 将NSDictionary类型的数据存储到本地
    // 1.找到对象存放的文件夹路径
    NSString *documentPath2 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    // 2.创建需要存储的字典
    NSDictionary *dictionaryPath = @{
                                     @"name" : @"leikun",
                                     @"age"  : @18,
                                     @"hobby": @"篮球",
                                     };
    
    // 3.创建字典存储最终路径
    NSString *dictionPath = [documentPath2 stringByAppendingPathComponent:@"leishen.plist"];
     // 4.将字典写入文件
    [dictionaryPath writeToFile:dictionPath atomically:YES];
    NSLog(@"dictionPath = %@", dictionPath);
    
    // 将存在本地的字典取出

    NSDictionary *newDictionary = [NSDictionary dictionaryWithContentsOfFile:dictionPath];
    NSLog(@"newDictionary = %@", newDictionary);
    
#pragma mark - 将NSData类型的数据存储到本地,(以图片为例)
    // 常用的两种初始化image的两种方式
    // 1.使用imageNamed: 第一次读取的时候,先把这个图片放到缓存里,下次再使用到这个同名图片的时候直接从缓存中读取;优点: 方便快捷,只有第一次使用的时候稍慢,接下来在使用就稍微快点;缺点: 如果在当前工程中只使用一次会浪费内存
    UIImage *image = [UIImage imageNamed:@"1.jpg"];
    
    // 2.使用initWithContentsOfFile初始化图片的时候,每次都会根据路径去读取,不会占用内存,如果图片在当前工程中只使用一次,应该选择这个方法
   
    //UIImage *image = [[UIImage alloc] initWithContentsOfFile:@"/Users/zhaoce/Documents/课件练习/UI进阶/第一节/UIsenior_1简单数据的存储/UIsenior_1简单数据的存储/1.jpg"];
    
  
    /**
     123.png
     123@2x.png
     123@3x.png
     图片的适配相关的内容
     */
    // 将image类型的对象转换成NSData类型的数据进行存储
    //使用UIImageJPEGRepresentation将图片转化成NSData类型的
    //第一个参数: 要转换的image对象
    //第二个参数: 表示图片压缩的值
    //iPhone中将大于2M的图片,会自动旋转90度,进行压缩处理,所以最终会将图片保存成jpeg格式
    
    NSData *imageData = UIImageJPEGRepresentation(image, 1);
    // 找到路径进行存储
    NSString *documentPath4 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    // 最终路径
    NSString *imagePath = [documentPath4 stringByAppendingString:@"/123.jpeg"];
    
    //写入文件
    [imageData writeToFile:imagePath atomically:YES];
    NSLog(@"imagePath = %@", imagePath);
    
      // 读取NSData类型的数据
    //需求: 将NSData类型的数据读取出来,转换成UIImage类型并显示在imageView上
    NSData *newData = [NSData dataWithContentsOfFile:imagePath];
    UIImage *showImage = [[UIImage alloc] initWithData:newData];
    UIImageView *showImageView = [[UIImageView alloc] initWithImage:showImage];
    [self.view addSubview:showImageView];
    
//    NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//    NSString *string = @"I love you";
//    
//    NSString *stringPath = [cachesPath stringByAppendingPathComponent:@"leikun.txt"];
//    [string writeToFile:stringPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
//    NSLog(@"stringPath = %@", stringPath);
//    NSString *newString = [NSString stringWithContentsOfFile:stringPath encoding:NSUTF8StringEncoding error:nil];
//    NSLog(@"newString = %@", newString);
    
//    NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//    NSArray *array1 = @[@"雷坤", @"雷神", @"雷军"];
//    NSString *arrayPath1 = [cachesPath stringByAppendingPathComponent:@"leikun.plist"];
//    [array1 writeToFile:arrayPath1 atomically:YES];
//    
//    NSLog(@"arrayPath1 = %@", arrayPath1);
//    
//    NSArray *newArray1 = [NSArray arrayWithContentsOfFile:arrayPath1];
//    NSLog(@"newArray1 = %@", newArray1);
    
    NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSDictionary *dic = @{
                          @"name" : @"Love",
                          @"gender" : @"you",
                          @"age" : @100
                        
                          };
    
    NSString *dicPath = [cachesPath stringByAppendingPathComponent:@"nvshen.plist"];
    
    [dic writeToFile:dicPath atomically:YES];
    
    NSLog(@"dicPath = %@", dicPath);
    
    NSDictionary *newDIC = [NSDictionary dictionaryWithContentsOfFile:dicPath];
    NSLog(@"newDIC = %@", newDIC);
    
    
    
    
    UIImage *image1 = [UIImage imageNamed:@"2.jpg"];
    
    NSData *ImageData = UIImageJPEGRepresentation(image1, 1);
    
    NSString *cachesPath1 = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    
    NSString *imagePath1 = [cachesPath1 stringByAppendingString:@"/234.jpeg"];
    [ImageData writeToFile:imagePath1 atomically:YES];
    
    NSLog(@"imagePath1 = %@", imagePath1);
    
    NSData *newData1 = [NSData dataWithContentsOfFile:imagePath1];
    UIImage *showImage1 = [[UIImage alloc] initWithData:newData1];
    UIImageView *showImageView1 = [[UIImageView alloc] initWithImage:showImage1];
    
    [self.view addSubview:showImageView1];
    
}

5.复杂对象的本地化

 

 

3.在Model对象的.h文件中声明属性

///姓名
@property (nonatomic, copy) NSString *name;

///性别
@property (nonatomic, copy) NSString *gender;

///年龄
@property (nonatomic, assign) NSInteger age;

// 语义设置一些内容;(15种)

4.在Model对象的.m文件中实现方法

//归档
//将所有的属性进行归档
- (void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:self.name forKey:@"name"];
    [aCoder encodeObject:self.gender forKey:@"gender"];
    [aCoder encodeInteger:self.age forKey:@"age"];
    
    
}

// 解档(反归档)
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];
    if (self) {
        self.name = [aDecoder decodeObjectForKey:@"name"];
        self.gender = [aDecoder decodeObjectForKey:@"gender"];
        self.age = [aDecoder decodeIntegerForKey:@"age"];
    }
    return self;
}

5.在ViewController中进行解档和归档

  //如何把一个Person类型的对象存入本地,即为复杂对象的本地化,这个对象必须遵守NSCoding协议,并实现协议中的两个方法
#pragma mark - 复杂对象的本地化
#pragma mark - 归档
    //1.找寻Documents文件夹的目录
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    // 2.创建Person对象
    Person *person = [[Person alloc] init];
    person.name = @"MBBoy";
    person.gender = @"男";
    person.age = 18;
    // 3.把这个复杂对象归档
    
    // 3.1:创建初始化NSMutableData,用于创建归档工具
    NSMutableData *data = [NSMutableData data];
    
    NSLog(@"======data = %@======", data);
    
    // 3.2:创建归档工具
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    // 3.3:对要归档的person对象进行归档
    [archiver encodeObject:person forKey:@"person"];
    // 3.4:结束归档
    [archiver finishEncoding];
    
     NSLog(@"======data = %@======", data);
    
    // 4.将归档的内容存储在本地
    NSString *personPath = [documentPath stringByAppendingPathComponent:@"person.plist"];
    [data writeToFile:personPath atomically:YES];
    NSLog(@"personPath = %@", personPath);
    
#pragma mark - 解档
    // 1.将要解档的数据找出
    NSData *resultData = [NSData dataWithContentsOfFile:personPath];
    NSLog(@"resultData = %@", resultData);
    
    // 2.创建解档工具
    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:resultData];
    // 3.对person对象进行解档[要使用对象接收]
    Person *newPerson = [unarchiver decodeObjectForKey:@"person"];
    // 4.结束解档
    [unarchiver finishDecoding];
    NSLog(@"name = %@, gender = %@, age = %ld", newPerson.name,newPerson.gender,newPerson.age);
    // 步骤记忆好
    // 直接写入本地: 数据是整存争取的
    
    
    
}

 

posted @ 2016-05-13 19:03  雷坤  阅读(503)  评论(0编辑  收藏  举报