UIday1801:沙盒三个文件的路径的获取、简单与复杂对象的读写、NSUserDefaults、NSFileManager

本文主要是介绍UIday1801:沙盒三个文件的路径的获取、简单与复杂对象的读写、NSUserDefaults、NSFileManager,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

初级数据持久化(沙盒三个文件的路径的获取、简单与复杂对象的读写、NSUserDefaults、NSFileManager  )

NSDocumentDirectory 是指程序中对应的Documents路径,而NSDocumentionDirectory对应于程序中的Library/Documentation路径,这个路径是没有读写权限的,所以看不到文件生成。

IOS开发是在沙盒中开发的,对一些部分的文件的读写进行了限制,只能在几个目录下读写文件:
 (1)Documents:应用中用户数据可以放在这里,iTunes备份和恢复的时候会包括此目录
 (2)tmp:存放临时文件,iTunes不会备份和恢复此目录,此目录下文件可能会在应用退出后删除
 (3)Library/Caches:存放缓存文件,iTunes不会备份此目录,此目录下文件不会在应用退出删除
 对于文件操作,NSSearchPathForDirectoriesInDomains是核心函数。


ViewController.m

#import "ViewController.h"
#import "Person.h"@interface ViewController ()@property(nonatomic,strong)UIImageView * imV;@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.// 沙盒 就是一个文件夹// *********获取沙盒路径************
/*// 获取用户名NSString * s = NSUserName();// 获取主路径NSString * rootPath = NSHomeDirectoryForUser(s);NSLog(@"%@",rootPath);*/// ************获取沙盒三个文件夹的路径***********/*// otherNSString * rootPath1 = NSHomeDirectory();NSLog(@"rootPath1 = %@",rootPath1);// 获取doucments路径// 作用 数据运行程序后产生,主要存储数据库等不常改变的数据文件。存在这里的文件会被备份。(下载的文件不能放在这里,如果放在这上传APPStore的时候会被拒掉)NSString * documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];NSLog(@"documentsPath = %@",documentsPath);// 获取Caches路径// 作用存放缓存文件,例如:音频、视频、图片(不会被自动备份)NSString * cachesPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];NSLog(@"cachesPath = %@",cachesPath);// 获取tmp路径// 作用:存放临时文件,程序下次启动不需要,退出清空。NSString * tmpPath = NSTemporaryDirectory();NSLog(@"tmpPath = %@",tmpPath);*///***********简单文件的写入*****************
/*//准备路径NSString * documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];NSLog(@"%@",documentsPath);//----------------NSString的写和读---------------//NSString  NSArray  NSDictionary  NSDataNSString * str1 = @"hello world";NSString * filePath = [documentsPath stringByAppendingString:@"/hello.txt"];//写入
//    [str1 writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];//读出NSString *s1 = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];NSLog(@"s1 = %@" ,s1);//------------- NSArray 的写和读 --------------//stringByAppendingPathComponent: 路径不需要写斜线,不需要写后缀NSString * filePath2 = [documentsPath stringByAppendingPathComponent:@"array"];NSArray *array1 = @[@"1",@"2",@"3"];//写入
//    [array1 writeToFile:filePath2 atomically:YES];//读NSArray * array2 = [NSArray arrayWithContentsOfFile:filePath2];//    NSLog(@"array2 = %@",array2);//------------- NSDictionary 的写和读------------NSString * filePath3 = [documentsPath stringByAppendingPathComponent:@"Dictionary"];//写入NSDictionary * dict = @{@"山西":@"太原",@"河北":@"石家庄"};[dict writeToFile:filePath3 atomically:YES];//读NSDictionary * dict2 = [NSDictionary dictionaryWithContentsOfFile:filePath3];NSLog(@"dict2  %@",dict2);//遍历字典for (id obj in dict2) {NSLog(@"dict2 %@ = %@",obj,[dict2 valueForKey:obj]);}*///*************** 复杂对象写入1 *****************
/*//创建对象Person * p1 = [[Person alloc]init];p1.name = @"贝爷";p1.age = 20;//准备NSMutableData 存数据NSMutableData * data = [NSMutableData data];创建归档工具NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];//开始归档[archiver encodeObject:p1 forKey:@"p1"];//完成归档[archiver finishEncoding];//准备路径NSString * documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];NSString * filePath4 = [documentsPath stringByAppendingPathComponent:@"贝爷.m"];NSLog(@"%@",documentsPath);//data写入
//    [data writeToFile:filePath4 atomically:YES];//读NSData * data1 = [NSData dataWithContentsOfFile:filePath4];NSLog(@"data1 = %@",data1);//反归档NSKeyedUnarchiver * unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:data1];//转换成对象Person * p2 = [unarchiver decodeObjectForKey:@"p1"];//反归档完成[unarchiver finishDecoding];NSLog(@"%@ == %ld",p2.name,p2.age);*///*************** 复杂对象写入2 *****************
/*//准备路径NSString * documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];//拼接路径NSString * filePath5 = [documentsPath stringByAppendingPathComponent:@"person.mp4"];//创建person对象Person *p3 = [[Person alloc]init];p3.name = @"六娃";p3.age = 18;//存
//    [NSKeyedArchiver archiveRootObject:p3 toFile:filePath5];//取 (这种归档方式,只能针对一个对象)Person * p4 = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath5];NSLog(@"%@ == %ld",p4.name,p4.age);*///************* NSUserDefaults 通常用来写引导页 ********
/*// 创建对象NSUserDefaults * ud = [NSUserDefaults standardUserDefaults];// 存[ud setObject:@"yan3" forKey:@"lanou"];// 同步数据[ud synchronize];// 取NSLog(@"%@",[ud objectForKey:@"lanou"]);*///************** 导航页测试 ****************
/*// 创建对象// NSUserDefaults 存一些简单的数据NSUserDefaults * ud = [NSUserDefaults standardUserDefaults];NSString *s = [ud objectForKey:@"first"];if (s == nil) {self.imV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"000.png"]];self.imV.frame = self.view.bounds;[self.view addSubview:_imV];[ud setObject:@"NO" forKey:@"first"];}*///*********** NSFileManager 文件管理对象 ************//路径NSString * cachesPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];NSLog(@"%@",cachesPath);//创建文件管理对象NSFileManager * fm = [NSFileManager defaultManager];//创建文件夹[fm createDirectoryAtPath:[cachesPath stringByAppendingPathComponent:@"00/test"] withIntermediateDirectories:YES attributes:nil error:nil];//更改文件名[fm moveItemAtPath:[cachesPath stringByAppendingPathComponent:@"00/test"] toPath:[cachesPath stringByAppendingPathComponent:@"Demo"] error:nil];//移动文件位置[fm moveItemAtPath:[cachesPath stringByAppendingPathComponent:@"00/test"] toPath:[cachesPath stringByAppendingPathComponent:@"Demo/test"] error:nil];//删除文件[fm removeItemAtPath:[cachesPath stringByAppendingPathComponent:@"Demo/test"] error:nil];//判断一个文件是否存在BOOL i = [fm fileExistsAtPath:@"Demo"];NSLog(@"%d",i);    }- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}@end

Person.h

#import <Foundation/Foundation.h>//一个类是否可以被归档反归档,要看是否遵循NSCoding协议。@interface Person : NSObject<NSCoding>@property(nonatomic,copy)NSString * name;@property(nonatomic,assign)NSInteger age;@end

Person.m

#import "Person.h"@implementation Person// 编码
- (void)encodeWithCoder:(NSCoder *)aCoder{[aCoder encodeObject:self.name forKey:@"p_name"];[aCoder encodeInteger:self.age forKey:@"p_age"];
}// 反编码
- (id)initWithCoder:(NSCoder *)aDecoder{if (self = [super init]) {self.name = [aDecoder decodeObjectForKey:@"p_name"];self.age = [aDecoder decodeIntegerForKey:@"p_age"];}return self;}@end


这篇关于UIday1801:沙盒三个文件的路径的获取、简单与复杂对象的读写、NSUserDefaults、NSFileManager的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/884626

相关文章

C# foreach 循环中获取索引的实现方式

《C#foreach循环中获取索引的实现方式》:本文主要介绍C#foreach循环中获取索引的实现方式,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、手动维护索引变量二、LINQ Select + 元组解构三、扩展方法封装索引四、使用 for 循环替代

Linux下如何使用C++获取硬件信息

《Linux下如何使用C++获取硬件信息》这篇文章主要为大家详细介绍了如何使用C++实现获取CPU,主板,磁盘,BIOS信息等硬件信息,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录方法获取CPU信息:读取"/proc/cpuinfo"文件获取磁盘信息:读取"/proc/diskstats"文

Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案

《Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案》:本文主要介绍Vue3组件中getCurrentInstance()获取App实例,但是返回nu... 目录vue3组件中getCurrentInstajavascriptnce()获取App实例,但是返回n

Python中判断对象是否为空的方法

《Python中判断对象是否为空的方法》在Python开发中,判断对象是否为“空”是高频操作,但看似简单的需求却暗藏玄机,从None到空容器,从零值到自定义对象的“假值”状态,不同场景下的“空”需要精... 目录一、python中的“空”值体系二、精准判定方法对比三、常见误区解析四、进阶处理技巧五、性能优化

SpringMVC获取请求参数的方法

《SpringMVC获取请求参数的方法》:本文主要介绍SpringMVC获取请求参数的方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下... 目录1、通过ServletAPI获取2、通过控制器方法的形参获取请求参数3、@RequestParam4、@

resultMap如何处理复杂映射问题

《resultMap如何处理复杂映射问题》:本文主要介绍resultMap如何处理复杂映射问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录resultMap复杂映射问题Ⅰ 多对一查询:学生——老师Ⅱ 一对多查询:老师——学生总结resultMap复杂映射问题

Python获取C++中返回的char*字段的两种思路

《Python获取C++中返回的char*字段的两种思路》有时候需要获取C++函数中返回来的不定长的char*字符串,本文小编为大家找到了两种解决问题的思路,感兴趣的小伙伴可以跟随小编一起学习一下... 有时候需要获取C++函数中返回来的不定长的char*字符串,目前我找到两种解决问题的思路,具体实现如下:

golang获取当前时间、时间戳和时间字符串及它们之间的相互转换方法

《golang获取当前时间、时间戳和时间字符串及它们之间的相互转换方法》:本文主要介绍golang获取当前时间、时间戳和时间字符串及它们之间的相互转换,本文通过实例代码给大家介绍的非常详细,感兴趣... 目录1、获取当前时间2、获取当前时间戳3、获取当前时间的字符串格式4、它们之间的相互转化上篇文章给大家介

使用Python开发一个简单的本地图片服务器

《使用Python开发一个简单的本地图片服务器》本文介绍了如何结合wxPython构建的图形用户界面GUI和Python内建的Web服务器功能,在本地网络中搭建一个私人的,即开即用的网页相册,文中的示... 目录项目目标核心技术栈代码深度解析完整代码工作流程主要功能与优势潜在改进与思考运行结果总结你是否曾经

Python获取中国节假日数据记录入JSON文件

《Python获取中国节假日数据记录入JSON文件》项目系统内置的日历应用为了提升用户体验,特别设置了在调休日期显示“休”的UI图标功能,那么问题是这些调休数据从哪里来呢?我尝试一种更为智能的方法:P... 目录节假日数据获取存入jsON文件节假日数据读取封装完整代码项目系统内置的日历应用为了提升用户体验,