沙盒
沙盒:每个iOS应用程序都会为自己创建一个文件系统目录(文件夹), 这个独立、封闭、安全的空间,叫做沙盒。
注:1.每个应用程序都会有一个应用程序沙盒
2.应用程序沙盒就是文件系统目录
沙盒机制:一种安全体系(安全机制)
沙盒机制特点:1.每个应用程序的活动范围都限定在自己的沙盒里
2.不能随意跨越自己的沙盒去访问别的应用程序沙盒中的内容(iOS8已经部分开放访问)
3.应用程序向外请求或接受数据都需要经过权限认证
沙盒目录下有三个文件夹Documents Library(下面有Caches和Preference目录) tmp
Documents目录
保存应用程序运行时生成的需要持久化的数据,Itunes会自动备份该目录
Library目录
存储程序的默认设置和其他状态信息,Itunes会自动备份该目录
1.Library/Caches:存放缓存文件,ITunes不会备份此目录,此目录下文件不会再应用程序退出删除。一般存放体积比较大,不是特别重要的资源。
2.Library/Preference:保存应用的所有偏好设置,iOS的Setting(设置)应用会在该目录中查找应用的设置信息,iTunes会自动备份该目录。注意:不应该直接创建偏好设置文件,而是应该使用NSUserDefaults类来取得和设置应用程序的偏好。
tmp目录
保存应用运行时所需要的临时数据,使用完毕后在将相应的文件从该目录删除。应用没有运行时,系统也有可能会清除该目录下的文件,iTunes不会同步该目录。iPhone重启时,该目录下的文件被删除。
获取沙盒路径
NSString *path = NSHomeDirectory();
获取沙盒下文件目录的路径
有三种获取方法:
1.拼接字符串:
NSString *Documents = [path stringByAppendingString:@"/Documents"];
2.拼接路径
NSString *Documents2 = [path stringByAppendingPathComponent:@"Documents"];
3.系统提供的获取沙盒下目录路径的方法
NSString *Documents3 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, NO) firstObject];
//NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, NO) 方法返回值为只有一个元素的数组,所以去第一个元素
对于其他两个文件路径的获取方式都一样,系统提供的方法有些区别
NSString *library3 = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject];
//对于Library文件获取路径方法,只是参数有些不一样
//获取library下的caches
NSString *cachesStr = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
获取tmp文件夹路径(系统提供的方法)
NSString *tmpStr = NSTemporaryDirectory();
获取程序包路径
NSString *appPath = [NSBundle mainBundle].resourcePath;
获取程序包内种的一个图片资源(apple.png)路径
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"apple" ofType:@"png"];
简单对象的读写(I/O)操作
简单对象:字符串,数组,字典,data
字符串存储
//获取存储的目录
NSString *str = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
//在沙盒文件夹下的Documents内创建.txt文件
NSString *newPath = [str stringByAppendingPathComponent:@"text.txt"];
//存入内容
NSString *name = @"蓝欧科技, nihao";
[name writeToFile:newPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
字符串读取
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *newPath = [path stringByAppendingString:@"/text.txt"];
NSString *string = [NSString stringWithContentsOfFile:newPath encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@", string);
数组存储
//获取路径
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
//创建文件
NSString *newPath = [path stringByAppendingPathComponent:@"array.plist"];
//存入的数组
NSArray *nameArr = @[@"zhangsan", @"lisi", @"wangwu"];
[nameArr writeToFile:newPath atomically:YES];
数组读取
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *newPath = [path stringByAppendingString:@"/array.plist"];
//获取数组
NSArray *array = [NSArray arrayWithContentsOfFile:newPath];
NSLog(@"%@", array);
字典存储
//获取路径
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
//新建plist文件
NSString *newPath = [path stringByAppendingPathComponent:@"dict.plist"];
//要存储的字典
NSArray *nameArr = @[@"zhangsan",@"lisi"];
NSDictionary *dict = @{@"a" : @"1", @"b":@"2", @"c" : @"3" , @"name" : nameArr};
// [dict setValue:nameArr forKey:@"name"];
[dict writeToFile:newPath atomically:YES];
字典读取
//获取路径
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
//获取plist路径
NSString *newPath = [path stringByAppendingString:@"/dict.plist"];
//获取字典
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:newPath];
NSLog(@"%@", dict);
NSData类型存储
NSString *str = @"nihoa";
//获取地址
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *dataPath = [path stringByAppendingPathComponent:@"data.plist"];
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
//写入文件
[data writeToFile:dataPath atomically:YES];
NSData读取
//获取路径
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *dataPath = [path stringByAppendingPathComponent:@"data.plist"];
NSData *data = [NSData dataWithContentsOfFile:dataPath];
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@", str);
图片存储
//将图片转成NSData类型在存储
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
//在沙盒问价下面创建一个文件
NSString *newPath = [path stringByAppendingPathComponent:@"image.png"];
//需要存储的图片
NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"tu.jpg"], 1.0);
//创建文件并存储
[imageData writeToFile:newPath atomically:YES];
图片读取
//获取沙盒地址
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
//获取图片data地址
NSString *newPath = [path stringByAppendingPathComponent:@"image.png"];
//获取data
NSData *data = [NSData dataWithContentsOfFile:newPath];
//获取图片
UIImage *image = [UIImage imageWithData:data];
//添加imageView
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 200, 150, 150)];
imageView.image = image;
[self.view addSubview:imageView];
总结起来就是:简单对象的写就是把在沙盒目录下新建一个文件,将简单对象写入进文件就行,读就是找到目标文件,根据路径创建对象就好
通过文件管理器来操作对象
创建对象
NSString *string = @"hello nihao";
//根据字符串创建data
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
创建文件管理器
NSFileManager *fileManager = [[NSFileManager alloc] init];
存储对象
//向目标文件写入信息
BOOL save = [fileManager createFileAtPath:[[self documentPath] stringByAppendingPathComponent:@"studenttt.plist"] contents:data attributes:nil];
if (save) {
NSLog(@"写入成功");
}else{
NSLog(@"写入错误");
}
//注:[self documentPath]是获取Documents路径
//获取Documents路径
- (NSString *)documentPath {
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
}
读取对象
NSData *mData = [fileManager contentsAtPath:[[self documentPath] stringByAppendingPathComponent:@"studenttt.plist"]];
NSString *mStr = [[NSString alloc] initWithData:mData encoding:NSUTF8StringEncoding];
移动文件
//移动文件
//atPath:文件存在当前目录
//toPath:目标目录
BOOL result = [fileManager moveItemAtPath:[[self documentPath] stringByAppendingPathComponent:@"studenttt.plist"] toPath:[[self libraryPath] stringByAppendingPathComponent:@"studenttt.plist"] error:nil];
if (result) {
NSLog(@"移动成功");
}else{
NSLog(@"移动错误");
}
//注:[self libraryPath] 获取library文件目录
//获取library路径
- (NSString *)libraryPath {
return [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject];
}
复制文件
BOOL result = [fileManager copyItemAtPath:[[self documentPath] stringByAppendingPathComponent:@"studenttt.plist"] toPath:[[self libraryPath] stringByAppendingPathComponent:@"studenttt.plist"] error:nil];
if (result) {
NSLog(@"复制成功");
}else{
NSLog(@"复制错误");
}
比较不同路径下的文件内容是否相同
BOOL result1 = [fileManager contentsEqualAtPath:[[self documentPath] stringByAppendingPathComponent:@"studenttt.plist"] andPath:[[self libraryPath] stringByAppendingPathComponent:@"studenttt.plist"]];
if (result1) {
NSLog(@"一致");
}else{
NSLog(@"不同");
}
文件是否存在
BOOL result = [fileManager fileExistsAtPath:[[self libraryPath] stringByAppendingPathComponent:@"studenttt.plist"]];
if (result) {
NSLog(@"存在");
}else{
NSLog(@"不存在");
}
移除文件
BOOL result = [fileManager removeItemAtPath:[[self libraryPath] stringByAppendingPathComponent:@"studenttt.plist"] error:nil];
if(result){
NSLog(@"移除成功");
}else{
NSLog(@"移除错误");
}
创建文件夹
BOOL result = [fileManager createDirectoryAtPath:[[self documentPath] stringByAppendingPathComponent:@"nimeide"] withIntermediateDirectories:YES attributes:nil error:nil];
if (result) {
NSLog(@"创建成功");
}else{
NSLog(@"创建错误");
}
复杂对象的读写---归档和反归档
复杂对象:在Foundation框架内不存在的数据类,如:自定义Person类无法再程序内通过writeToFile:这个方法写入到文件内
如何将复杂对象写入文件?
复杂对象无法通过writeFile:方法进行数据持久化,只能通过将复杂对象转化为NSData(这个步骤就是归档),然后再通过writeTiFile:写入文件
如何从文件中读取复杂对象?
从文件中读取NSData数据,将NSdata转化为复杂对象(这个步骤就是反归档)
注:
1.复杂对象写入文件的过程(复杂对象->归档->NSData->writeToFile:)
2.从文件中读取出复杂对象过程(读取文件->NSData->反归档->复杂对象)
如何进行归档和反归档
1.首先,复杂对象所属的类要遵循<NSCoding>协议
//Person.h
@interface Person : NSObject <NSCoding>
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *gender;
@property (nonatomic, assign) NSInteger age;
@end
2.其次,实现协议中的两个方法
- -(void)encodeWithCoder:(NSCoder *)aCoder;
- -(instancetype)initWithCoder:(NSCoder *)aDecoder
//Person.m
@implementation Person
//当对象进行归档操作的时候,会自动调用这个方法
- (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{
if (self = [super init]) {
self.name = [aDecoder decodeObjectForKey:@"name"];
self.age = [aDecoder decodeIntegerForKey:@"age"];
self.gender = [aDecoder decodeObjectForKey:@"gender"];
}
return self;
}
@end
归档 ----NSKeyedArchiver
Person *person = [[Person alloc] init];
person.name = @"小明";
person.age = 23;
person.gender = @"男";
//进行归档操作
//1.创建一个NSmutableData,用来存储归档后的数据
NSMutableData *mData = [[NSMutableData alloc] init];
//2.创建归档器
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:mData];
//进行归档
[archiver encodeObject:person forKey:@"person"];
//归档结束
[archiver finishEncoding];
//存到沙盒的文件夹中
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *newPath = [path stringByAppendingPathComponent:@"guidang.plist"];
//存储data
[mData writeToFile:newPath atomically:YES];
反归档 ----NSKeyedUnarchiver
//获取路径
NSString * newPath = [[self documentPath] stringByAppendingPathComponent:@"guidang.plist"];
//取出数据
NSData *data = [NSData dataWithContentsOfFile:newPath];
//创建反归档器
NSKeyedUnarchiver *unArchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
Person *person = [unArchiver decodeObjectForKey:@"person"];
//反归档结束
[unArchiver finishDecoding];
NSLog(@"name : %@ age : %ld gender : %@", person.name, person.age, person.gender);
网友评论