首页 > 其他分享 >NSSearchPathForDirectoriesInDomains用法

NSSearchPathForDirectoriesInDomains用法

时间:2022-12-09 21:33:19浏览次数:58  
标签:paths Documents 文件 用法 NSSearchPathForDirectoriesInDomains NSString YES 目录

1.
iPhone会为每一个应用程序生成一个私有目录,这个目录位于:
/Users/sundfsun2009/Library/Application Support/iPhone Simulator/User/Applications下,
并随即生成一个数字字母串作为目录名,在每一次应用程序启动时,这个字母数字串都是不同于上一次。

所以通常使用Documents目录进行数据持久化的保存,而这个Documents目录可以通过:
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserdomainMask,YES) 得到。
代码如下:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(@"path: %@",path);
打印结果如下:
path: /Users/apple/Library/Application Support/iPhone Simulator/4.3/Applications/550AF26D-174B-42E6-881B-B7499FAA32B7/Documents

而通过 NSHomeDirectory()也可以得到程序的目录,代码如下:
NSString *destPath = NSHomeDirectory();
NSLog(@"path: %@",destPath);
打印结果如下:
path: /Users/apple/Library/Application Support/iPhone Simulator/4.3/Applications/550AF26D-174B-42E6-881B-B7499FAA32B7
看看两者打印出来的结果,我们可以看出这两种方法的不同



2.

这个主要就是返回一个绝对路径用来存放我们需要储存的文件。
- (NSString *)dataFilePath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:@"shoppingCar.plist"];
}
NSFileManager* fm=[NSFileManager defaultManager];
if(![fm fileExistsAtPath:[self dataFilePath]]){
//下面是对该文件进行制定路径的保存
[fm createDirectoryAtPath:[self dataFilePath] withIntermediateDirectories:YES attributes:nil error:nil];
//取得一个目录下得所有文件名
NSArray *files = [fm subpathsAtPath: [self dataFilePath] ];
//读取某个文件
NSData *data = [fm contentsAtPath:[self dataFilePath]];
//或者
NSData *data = [NSData dataWithContentOfPath:[self dataFilePath]];
}




3.

因为应用是在沙箱(sandbox)中的,在文件读写权限上受到限制,只能在几个目录下读写文件:
• Documents:应用中用户数据可以放在这里,iTunes备份和恢复的时候会包括此目录
• tmp:存放临时文件,iTunes不会备份和恢复此目录,此目录下文件可能会在应用退出后删除
• Library/Caches:存放缓存文件,iTunes不会备份此目录,此目录下文件不会在应用退出删除
在Documents目录下创建文件
代码如下:
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory
, NSUserDomainMask
, YES);
NSLog(@"Get document path: %@",[paths objectAtIndex:0]);NSString *fileName=[[paths objectAtIndex:0] stringByAppendingPathComponent:@"myFile"];
NSString *content=@"a";
NSData *contentData=[content dataUsingEncoding:NSASCIIStringEncoding];
if ([contentData writeToFile:fileName atomically:YES]) {
NSLog(@">>write ok.");
} 可以通过ssh登录设备看到Documents目录下生成了该文件。

上述是创建ascii编码文本文件,如果要带汉字,比如:
NSString *fileName=[[paths objectAtIndex:0] stringByAppendingPathComponent:@"myFile"];
NSString *content=@"更深夜静人已息";
NSData *contentData=[content dataUsingEncoding:NSUnicodeStringEncoding];
if ([contentData writeToFile:fileName atomically:YES]) {
NSLog(@">>write ok.");
} 如果还用ascii编码,将不会生成文件。这里使用NSUnicodeStringEncoding就可以了。
通过filezilla下载到创建的文件打开,中文没有问题:
​​​​
在其他目录下创建文件
如果要指定其他文件目录,比如Caches目录,需要更换目录工厂常量,上面代码其他的可不变:
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSCachesDirectory
, NSUserDomainMask
, YES);
使用NSSearchPathForDirectoriesInDomains只能定位Caches目录和Documents目录。
tmp目录,不能按照上面的做法获得目录了,有个函数可以获得应用的根目录:
NSHomeDirectory()
也就是Documents的上级目录,当然也是tmp目录的上级目录。那么文件路径可以这样写:
NSString *fileName=[NSHomeDirectory() stringByAppendingPathComponent:@"tmp/myFile.txt"];
或者,更直接一点,可以用这个函数:
NSTemporaryDirectory()
不过生成的路径将可能是:
…/tmp/-Tmp-/myFile.txt
使用资源文件
在编写应用项目的时候,常常会使用资源文件,比如:
​​​​
安装到设备上后,是在app目录下的:
​​​​
以下代码演示如何获取到文件并打印文件内容:
NSString *myFilePath = [[NSBundle mainBundle]
pathForResource:@"f"
ofType:@"txt"];
NSString *myFileContent=[NSString stringWithContentsOfFile:myFilePath encoding:NSUTF8StringEncoding error:nil];
NSLog(@"bundel file path: %@ \nfile content:​​%@",myFilePath,myFileContent​​);
代码运行效果:
​​​​

标签:paths,Documents,文件,用法,NSSearchPathForDirectoriesInDomains,NSString,YES,目录
From: https://blog.51cto.com/u_15907570/5926602

相关文章

  • wlan_cli用法
    #启动脚本wpa_supplicant-iwlan0-Dnl80211-c/etc/ambaipcam/IPC_Q313/config/wlan/wpa_supplicant.conf-B#查询网卡状态wpa_cli-p/var/run/wpa_supplicant-iwlan0st......
  • QuickContactBadge的用法
    首先上个运行效果图: 下边是ListView中单个Item的布局文件:<?xmlversion="1.0"encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/ap......
  • OC之【NSString字符串的其他用法】
    #import<Foundation/Foundation.h>字符串的大小写处理voidNSString*str=@"GuangDong";//转成大写NSLog(@"大写:%@",[struppercaseString]);//转成小......
  • Objective-C #define 用法
    在C语言中,预处理代码(Preprocessor)是非常强大的工具,能让你的代码变得更加易读和易改。利用预处理代码,你可以重新定义代码的一部分,使得你的代码更适合你的风格。预处理......
  • OC之【@property的用法】
    1.这里的retain代表:在set方法中,release旧值,retain新值(nonatomic,retain)Book*book;(retain)Card*card;代表只生成get方法的声明默认是readwrite,同时生成get和set......
  • Linux基础知识(12)- GCC 简单使用(二)| Makefile 的高级用法
    在“Linux基础知识(11)-GCC简单使用(一)|GCC安装配置和Makefile的基本用法”里我们演示了GCC安装配置和Makefile的基本用法,本文将继续演示Makefile的高级用法。......
  • Linux基础知识(11)- GCC 简单使用(一)| GCC 安装配置和 Makefile 的基本用法
    GCC的全拼为GNUCCompiler,即GUN计划诞生的C语言编译器,显然最初GCC的定位确实只用于编译C语言。但经过这些年不断的迭代,GCC的功能得到了很大的扩展,它不仅可以用......
  • 一个很好的时间日期插件用法
    my97日期插件是一个非常好用、功能非常强大的日期控件,简单示例如下:静态限制注意:日期格式必须与realDateFmt和realTimeFmt一致而不是与dateFmt一致你可以给通过配......
  • dwr笔记二之经典用法之和spring结合+验证用户是否存在
    springmvc+DWR验证用户名是否存在,是最经典的案例了.1在DWR2里,注意配置的类名跟DWR1不同了由uk.ltd.getahead变成了org.directwebremoting。换上了新的配置1<ser......
  • springboot整合mongodb MongoTemplate和MongoRepository的用法
    前情Springboot是最简单的使用Spring的方式,而MongoDB是最流行的NoSQL数据库。两者在分布式、微服务架构中使用率极高,本文将用实例介绍如何在Springboot中整合MongoDB的两种......