UI沙盒机制
每个应用程序位于文件系统的严格限制部分
每个应用程序只能为该程序创建的文件系统中读取文件
每个应用程序在IOS系统内都放在了统一的文件夹目录下
1.通过Finder查找程序沙盒相对路径/Library/Application Support/Iphone Simulator
//获取Documents路径
- (void)searchDocumentsPath{
//通过代码查找沙盒相对路径
NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSLog(@"path ====== %@",paths);
}
- (void)writeString{
//写入路径
NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory,NSUserDomainMask, YES);
//提取字符串
NSString *str = [paths firstObject];
//字符串的拼接
str = [NSStringstringWithFormat:@"%@%@",str,@"/text.txt"];
//写
NSString *value = @"范厨师捡黑板";
NSError *error = nil;
[value writeToFile:stratomically:YESencoding: NSUTF8StringEncodingerror:&error];//**取地址
if (error) {
NSLog(@"\nerror == %@\n",error);
}
//写入文件
//获取所有DOCUMENTS文件夹
NSArray *documentList =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
//将最后一个documents文件夹路径作为文件的存储目录
NSString *documentPath = documentList.lastObject;
//字符串拼接
documentPath = [NSStringstringWithFormat:@"%@%@",documentPath,@"text1.txt"];
//写
NSString *value1 = @"瞌睡瞌睡不能睡觉";
NSError *error1 = nil;
[value1 writeToFile:documentPathatomically:YESencoding:NSUTF8StringEncodingerror:&error1];
if (error1) {
NSLog(@"\nerror1 === %@\n",error1);
}
//数组对象写入
//构造数组plist文件的存储路径
NSArray *arrayList =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *arrayDocumentPath = arrayList.lastObject;
arrayDocumentPath = [NSStringstringWithFormat:@"%@%@",arrayDocumentPath,@"array.txt"];
// NSString *arrayPlish = [documentPath stringByAppendingString:array.plists];
NSArray *foo_array = @[@"cyy",@"yhy",@"xdp",@"lmz",@"zdh"];
[foo_arraywriteToFile:arrayDocumentPath atomically:YES ];
NSLog(@"%@",foo_array);
//字典对象写入文件
NSArray *arrayListdic =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *dicPath = arrayListdic.lastObject;
dicPath = [NSStringstringWithFormat:@"%@%@",dicPath,@"/dic.plist"];
NSDictionary *foo_dic = @{@"jz":@"cyy",@"jzzl":@"lmz",@"jfz":@"zdh"};
[foo_dicwriteToFile:dicPath atomically:YES];
}
- (NSArray *)getPathOfDocuments{
NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
return paths;
}
#pragma mark 读取字符串
- (void)getString{
//存储路径
NSString *str = [NSStringstringWithFormat:@"%@%@",[[selfgetPathOfDocuments] firstObject],@"/text.txt"];
NSError *error = nil;
//将字符串存入document,以path.txt命名
NSString *string = [[NSStringalloc]initWithContentsOfFile:strencoding:NSUTF8StringEncodingerror:&error];
if(error){
NSLog(@"%@",error);
}else {
NSLog(@"%@",string);
}
}
#pragma mark 写入数组
- (void)writeArray{
NSString *str = [NSStringstringWithFormat:@"%@%@",[[selfgetPathOfDocuments] firstObject],@"/array.txt"];
NSArray *array = [NSArrayarrayWithObjects:@"1",@"1",@"1",@"1",@"1",nil];
[array writeToFile:stratomically:YES];
}
#pragma mark 读取数组
- (void)getArray{
NSString *str = [NSStringstringWithFormat:@"%@%@",[[selfgetPathOfDocuments] firstObject],@"/array.txt"];
NSArray *array = [[NSArrayalloc]initWithContentsOfFile:str];
// NSArray *arr = [NSArray arrayWithContentsOfFile:str];
NSLog(@"%@",array);
}
#pragma mark 写入字典
- (void)writeDictionary{
NSString *str = [NSStringstringWithFormat:@"%@%@",[[selfgetPathOfDocuments] firstObject],@"/dic.txt"];
NSDictionary *dic = [NSDictionarydictionaryWithObjectsAndKeys:@"hehehe",@"1",@"hahaha",@"2",@"enenen",@"3",nil];
[dic writeToFile:stratomically:YES];
}
#pragma mark 读取字典
- (void)getDictionary{
NSString *str = [NSStringstringWithFormat:@"%@%@",[[selfgetPathOfDocuments] firstObject],@"/dic.txt"];
NSDictionary *dic = [NSDictionarydictionaryWithContentsOfFile:str];
NSLog(@"%@",dic);
}
- (void)writeData{
NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *str = [paths firstObject];
str = [NSStringstringWithFormat:@"%@/data",str];
//只能和nsstring的字符串进行转换
NSString *value =@"a ni ha se you ";
//string 转化为nsdata
NSData *data = [valuedataUsingEncoding:NSUTF8StringEncoding];//转化为NSUTF8StringEncoding二进制编码格式
[data writeToFile:stratomically:YES];
}
- (void)readData{
NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *str = [paths firstObject];
str = [NSStringstringWithFormat:@"%@/data",str];
NSData *data = [NSDatadataWithContentsOfFile:str];
//nsdata转化为字符串类型
NSString *value = [[NSStringalloc]initWithData:dataencoding:NSUTF8StringEncoding];
NSLog(@"11111111%@",value);
}
//轻量级的存储 standardUserDefaults单例内存唯一
- (void)userDefaultsWrite{
NSUserDefaults *defaults = [NSUserDefaultsstandardUserDefaults];
//和字典一样存
[defaultssetObject:@"不得不说" forKey:@"name"];
//同步存储
[defaultssynchronize];
}
- (void)readUserDefaults{
NSString *value = [[NSUserDefaultsstandardUserDefaults]objectForKey:@"name"];
}
//归档 即将自己创建的model类对象转化为二进制格式,通过二进制格式进行存储
- (void)archiveObject{
Student *stu = [[Studentalloc]init];
stu.name =@"理想";
stu.sex =@"女";
stu.age = 23;
NSArray *arr = [NSArrayarrayWithObject:stu];
//归档存数组
NSData *data = [NSKeyedArchiverarchivedDataWithRootObject:arr];
NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *str = [paths firstObject];
str = [NSStringstringWithFormat:@"%@/model",str];
[data writeToFile:stratomically:YES];
NSLog(@"%@",data);
}
//解档 将归档后的二进制解档为相对应的model 类型对象
- (void)unarchiveObject{
NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *str = [paths firstObject];
str = [NSStringstringWithFormat:@"%@/model",str];
NSData *data = [NSDatadataWithContentsOfFile:str];
//解档必须也要数组接收
NSArray *array = [NSKeyedUnarchiverunarchiveObjectWithData:data];
NSLog(@"11232323%@",[arrayfirstObject]);
}
- (instancetype)initWith:(NSString *)name sex:(NSString *)sex age:(NSInteger )age{
self = [superinit];
if (self) {
self.name = name;
self.sex = sex;
self.age =age;
}
return self;
}
//归档协议 序列化协议
- (void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:self.nameforKey:@"name"];
[aCoder encodeObject:self.sexforKey:@"sex"];
//先转化为number类型
NSNumber *num = [NSNumbernumberWithInt:self.age];
[aCoderencodeObject:num forKey:@"age"];
}
// 解档,反序列化
- (id)initWithCoder:(NSCoder *)aDecoder{
if (self = [superinit]) {
//aDecoder反编码
self.name = [aDecoderdecodeObjectForKey:@"name"];
self.sex = [aDecoderdecodeObjectForKey:@"sex"];
NSNumber *num = [aDecoder decodeObjectForKey:@"age"];
self.age = [numintValue];
}
return self;
}