沙盒机制

来源:互联网 发布:软件测试用例实例 编辑:程序博客网 时间:2024/05/20 06:56

需要在建的属性里签编码与反编码的协议

@interface Student :NSObject<NSCoding>

之后建所需要的属性

@property(nonatomic,copy)NSString *name;

@property(nonatomic,copy)NSString *sex;


在类的m文件中

#pragma mark 进行编码

- (void)encodeWithCoder:(NSCoder *)aCoder{

    [aCoder encodeObject:self.nameforKey:@"name"];

    [aCoder encodeObject:self.sexforKey:@"sex"];

    


}

#pragma mark 反编码

- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder{

    self = [superinit];

    if (self) {

        self.name = [aDecoderdecodeObjectForKey:@"name"];

        self.sex  = [aDecoderdecodeObjectForKey:@"sex"];

    }

    return self;

}


获取沙盒机制中几种不同的路径

 //Documents:用来存储缓存数据,比如,音频,视频,或请求下来的数据.

    //Library下的Caches:用来存储系统的缓存.

    //Library下的Preferences:用来存储系统的偏好设置数据

    //temp:用来存储临时文件,应用重启后会消失.

    

    //获取Documents路径

    /*

    NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;

    NSLog(@"%@",documentPath);

    */

    

    //获取tmp路径

    /*

    NSString *tmpPath = NSTemporaryDirectory();

    NSLog(@"%@",tmpPath);

    */

    

    //获取library路径

    /*

    NSString *libraryPath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).lastObject;

    NSLog(@"%@",libraryPath);

    */

    

    //Library下的Caches的路径

    /*

    NSString *libraryCachesPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;

    NSLog(@"%@",libraryCachesPath);

    */

    

    //Library下的Preferences的路径

    NSString *libraryPreferencesPath =NSSearchPathForDirectoriesInDomains(NSPreferencePanesDirectory,NSUserDomainMask, YES).lastObject;

    NSLog(@"%@",libraryPreferencesPath);


往沙盒中存入简单的对象

//1.将字符串写入文件

    NSString *string = @"smallGuang";

    //(1)拼接写入的路径

    //这个方法会自动在我们字符串的前面加一个"/"

    NSString *strPath = [path stringByAppendingPathComponent:@"string.txt"];

    //(2)将字符串写入文件

    [string writeToFile:strPath atomically:YES encoding:NSUTF8StringEncoding error:nil];

    //(3)取出已经写入的字符串

    NSString *str = [NSString stringWithContentsOfFile:strPath encoding:NSUTF8StringEncoding error:nil];

    NSLog(@"%@",str);

     

    //人们都有一些记忆,可以说是记忆们也可以说是梦境

  

    


//将数组写入文件

    NSArray *array = @[@"heihei",@"haha"];

    //(1)拼接路径

    NSString *arrPath = [path stringByAppendingPathComponent:@"array.xml"];

    //(2)将数组写入文件

    [array writeToFile:arrPath atomically:YES];

    //(3)读取数组文件

    NSArray *resultArray = [NSArray arrayWithContentsOfFile:arrPath];

    NSLog(@"%@",resultArray);


    


//将字典持久化

    NSDictionary *dic = @{@"name":@"smallGuang",@"sex":@"man"};

    //(1)拼接路径

    NSString *dicPath = [path stringByAppendingPathComponent:@"dic.plist"];

    //(2)写入文件

    [dic writeToFile:dicPath atomically:YES];

    //(3)读取文件

    NSDictionary *resultDic = [NSDictionary dictionaryWithContentsOfFile:dicPath];

    NSLog(@"%@",resultDic);

    

 


    

//图片存在本地

    UIImage *image = [UIImage imageNamed:@".jpg"];

    self.imageOne.image = image;

    //拼接路径

    NSString *imagePath = [path stringByAppendingPathComponent:@".jpg"];

    //image类型转换成data类型

    NSData *data = UIImagePNGRepresentation(image);

    //存入文件

    [data writeToFile:imagePath atomically:YES];

    //取出图片文件

    NSData *newData = [NSData dataWithContentsOfFile:imagePath];

    UIImage *newImage = [UIImage imageWithData:newData];

    self.inageTwo.image = newImage;


*********

向沙盒写入文件以及往出取值的过程

NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES).lastObject;

    NSLog(@"%@",path);

    

    //自定义文件的存储(归档 反规档)

    Student *stu = [[Studentalloc] init];

    stu.name = @"咣咣";

    stu.sex = @"smallMan";

    

    NSMutableData *data= [NSMutableDatadata];

    

    //创建归档器

    NSKeyedArchiver *archiver = [[NSKeyedArchiveralloc] initForWritingWithMutableData:data];

    //进行归档

    [archiver encodeObject:stu forKey:@"student"];

    //结束归档

    [archiver finishEncoding];

    

    //将归档后的data文件存入沙盒中

    //拼路径

    NSString *dataPath = [pathstringByAppendingPathComponent:@"stu.txt"];

    //通过路径写入文件

    [data writeToFile:dataPath atomically:YES];

    

    //取值过程

    NSMutableData *resultData = [NSMutableDatadataWithContentsOfFile:dataPath];

    

    //创建反归档器

    NSKeyedUnarchiver *unArchiver = [[NSKeyedUnarchiveralloc] initForReadingWithData:resultData];

    //进行反归档

    Student *resultStu = [unArchiver decodeObjectForKey:@"student"];

    [unArchiver finishDecoding];

    NSLog(@"%@",resultStu.name);


文件管理器

// 文件管理器

    NSFileManager *maneger = [NSFileManagerdefaultManager];

    NSString *path =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES).lastObject;

    NSLog(@"%@",path);

    NSString *smallGuang = [pathstringByAppendingPathComponent:@"xiaoGUang"];

    

    //使用文件管理器创建文件夹

    [maneger createDirectoryAtPath:smallGuangwithIntermediateDirectories:YESattributes:nilerror:nil];

    

    NSString *string = @"小光";

    NSString *xiaoGuangPath = [smallGuang stringByAppendingPathComponent:@"gunag.txt"];

    [string writeToFile:xiaoGuangPathatomically:YESencoding:NSUTF8StringEncodingerror:nil];





1 0