SandBox
2016-08-05 本文已影响0人
不高冷的龙拾柒
沙盒(SandBox)包括Documents、Library及tmp,其中Library又包含Caches和Preferences,如下图所示:
沙盒的构成
1.具体的作用:
通俗地讲:Documents 就是用来给用户主动存储数据的文件文档;Library呢就是用来给程序员存储的资源, 其中,Caches是装缓存文件的,Preferences里放用户信息,用户设置等;tmp则是临时目录,用来存储下载的临时文件的.
2.获取沙盒路径:
运用到的是C语言函数.
(1)
如获取根目录:
NSLog( @"Home------%@", NSHomeDirectory());
临时目录:
NSLog(@"Temporary------%@", NSTemporaryDirectory());
(2)
常用的获取Document、以及Caches和Library的方法如下:
NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSLog(@"PathArray-------------%@", [pathArray firstObject]);
参数1含义是搜索对应文件夹路径,可根据需要填入NSDocumentDirectory、 NSCachesDirectory以及NSLibraryDirectory;
参数2含义是在用户作用域下搜索;
参数3含义是:YES表示绝对路径,即可以获取到完整的路径,而NO则表示的是相对路径,即获取到的是较为不完整的路径(当打印路径时,路径前会有~修饰).
(3)
获取.app文件包的方法
NSLog(@"NSBundle---%@", [NSBundle mainBundle]);
3.简单的文件存储(Input/Output)
(1)将字符串写入本地
NSString *helloString= @[@"Hello, I/O File"];
NSString *writePath = [[pathArray firstObject] stringByAppendingPathComponent:@"hello.text"];
NSError *error = nil;
BOOL isSuccess = [helloString writeToFile:writePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
if (error) {
NSLog(@"fail");
} else {
NSLog(@"success");
}
读取字符串
NSString *readError = nil;
NSString *readString =[NSString stringWithContentsOfFile:writePath encoding:NSUTF8StringEncoding error:&readError];
NSLog(@"%@", readString);
(2)将数组写入本地
NSArray *array = @[@"FLL"];
NSString *arrayPath = [[pathArray firstObject] stringByAppendingPathComponent:@"name.plist"];
BOOL isArrayWriteSuccess = [array writeToFile:arrayPath atomically:YES];
if (isArrayWriteSuccess) {
NSLog(@"success");
} else {
NSLog(@"failure");
}
读取数组
NSArray *nameArray = [NSArray arrayWithContentsOfFile:arrayPath];
NSLog(@"%@", nameArray);
(3)将字典写入本地
NSDictionary *dictionary = @{@"name":@"guo"};
NSString *dicPath = [[pathArray firstObject] stringByAppendingPathComponent:@"dic.plist"];
BOOL isDicSuccess = [dictionary writeToFile:dicPath atomically:YES];
if (isArrayWriteSuccess) {
NSLog(@"成功");
} else {
NSLog(@"失败");
}
读取字典
NSDictionary *dicContent = [NSDictionary dictionaryWithContentsOfFile:dicPath];
NSLog(@"dic%@", dicContent);
(4)将Data类型写入本地
UIImage *image = [UIImage imageNamed:@"照片"];
将图片转化成二进制(UIImageJPEGRepresentation()/UIImagePNGRepresentation())
NSData *imageData = UIImageJPEGRepresentation(image, 0.1);
其中0.1为缩放比例,可不写则默认为不进行缩放
NSString *dataPath = [[pathArray firstObject] stringByAppendingPathComponent:@"imageData"];
BOOL isDataWriteSuccess = [imageData writeToFile:dataPath atomically:YES];
if (isDataWriteSuccess) {
NSLog(@"OK!");
} else {
NSLog(@"NO OK!");
}
读取Data类型
NSData *imageNewData = [NSData dataWithContentsOfFile:dataPath];
NSLog(@"image:%@", imageData);
UIImage *fileImage = [UIImage imageWithData:imageData];
4.复杂对象文件读写, 自定义类型(归档/反归档,也称序列化/反序列化)
归档:将对象存储到本地
Book *book = [Book new];
book.bookName = @"iOS从入门到放弃";
book.bookType = @"教育";
book.bookPrice = @"64";
book.bookAuthor = @"郭宝";
book.bookAddress = @"郭宝黑科技出版社";
NSString *bookPath = [[pathArray firstObject] stringByAppendingPathComponent:@"book.plist"];
BOOL isBookWriteSuccess =[NSKeyedArchiver archiveRootObject:book toFile:bookPath];
if (isBookWriteSuccess) {
NSLog(@"欧耶, 你赢了!");
} else {
NSLog(@"日了狗了!");
}
反归档:
Book *huangBook = [NSKeyedUnarchiver unarchiveObjectWithFile:bookPath];
NSLog(@"%@", huangBook.bookName);
//
// Book.h
// 17-SandBox
//
// Created by 郭宝 on 16/8/5.
// Copyright © 2016年 郭宝. All rights reserved.
//
#import <Foundation/Foundation.h>
// 1.需要归档的类, 需要签订和实现 <NSCoding> 协议
@interface Book : NSObject <NSCoding>
@property (nonatomic, copy) NSString *bookName;
@property (nonatomic, copy) NSString *bookPrice;
@property (nonatomic, copy) NSString *bookAuthor;
@property (nonatomic, copy) NSString *bookAddress;
@property (nonatomic, copy) NSString *bookType;
@end
//
// Book.m
// 17-SandBox
//
// Created by 郭宝 on 16/8/5.
// Copyright © 2016年 郭宝. All rights reserved.
//
#import "Book.h"
@implementation Book
// 取值
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super init];
if (self) {
_bookName = [aDecoder decodeObjectForKey:@"bookName"];
_bookPrice = [aDecoder decodeObjectForKey:@"bookPrice"];
_bookAddress = [aDecoder decodeObjectForKey:@"bookAddress"];
_bookType = [aDecoder decodeObjectForKey:@"bookType"];
_bookAuthor = [aDecoder decodeObjectForKey:@"bookAuthor"];
}
return self;
}
// 赋值, 存储
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:_bookName forKey:@"bookName"];
[aCoder encodeObject:_bookAddress forKey:@"bookAddress"];
[aCoder encodeObject:_bookPrice forKey:@"bookPrice"];
[aCoder encodeObject:_bookType forKey:@"bookType"];
[aCoder encodeObject:_bookAuthor forKey:@"bookAuthor"];
}
接下来Finder->前往文件夹->粘贴PathArray->前往.
PathArray.png
前往文件夹.png
EFF5A234-971E-48C0-9205-364316712D54.png