ios系列——文件路劲NSFileManager

本文介绍了使用Objective-C获取iOS模拟器中应用程序沙盒路径的方法,并通过实例展示了如何访问Documents、Library、Caches等关键目录。

NSString *path = NSHomeDirectory();

上面的代码得到的是应用程序目录的路径,在该目录下有三个文件夹:Documents、Library、temp以及一个.app包!

该目录下就是应用程序的沙盒,应用程序只能访问该目录下的文件夹!!!

请参考下面的例子:

1

NSString *path1 = NSHomeDirectory();
NSLog(@"path1:%@", path1);

path1:/Users/xxx/Library/Application Support/iPhone Simulator/4.2/Applications/172DB70A-145B-4575-A31E-D501AC6EA830

2、

NSString *path2 = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];


NSLog(@"path2:%@", path2);

path2:/Users/xxx/Library/Application Support/iPhone Simulator/4.2/Applications/172DB70A-145B-4575-A31E-D501AC6EA830/Library/Caches

3、

NSString *path3 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

NSLog(@"path3:%@", path3);

path3:/Users/xxx/Library/Application Support/iPhone Simulator/4.2/Applications/172DB70A-145B-4575-A31E-D501AC6EA830/Documents

4、

NSString *path4 = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];

NSLog(@"path4:%@", path4);

path4:/Users/xxx/Library/Application Support/iPhone Simulator/4.2/Applications/172DB70A-145B-4575-A31E-D501AC6EA830/Documents

5、

NSString *path5 = [NSHomeDirectory() stringByAppendingPathComponent:@"Library"];

NSLog(@"path5:%@", path5);

path5:/Users/xxx/Library/Application Support/iPhone Simulator/4.2/Applications/172DB70A-145B-4575-A31E-D501AC6EA830/Library

6、

NSString *path6 = [NSHomeDirectory() stringByAppendingPathComponent:@"temp"];

NSLog(@"path6:%@", path6);

path6:/Users/xxx/Library/Application Support/iPhone Simulator/4.2/Applications/172DB70A-145B-4575-A31E-D501AC6EA830/temp
### 如何在 PyCharm 中设置或找到文件路径 #### 使用 `idea.properties` 文件自定义配置路径 当启动 PyCharm 并选择 Help -> Edit Custom Properties 选项时,会弹出一个窗口询问是否要在默认位置创建一个 `idea.properties` 文件[^1]。通过选择 Create 可以编辑此文件来更改多个重要路径: - 配置路径 (`idea.config.path`) - 系统路径 (`idea.system.path`) - 插件路径 (`idea.plugins.path`) - 日志路径 (`idea.log.path`) 这些路径可以被更改为其他磁盘上的特定目录,例如 G:\software\Pycharm\PyCharm 2021.2.2 下的不同子文件夹。 ```properties idea.config.path=G:/software/Pycharm/PyCharm 2021.2.2/configuration_files/.PyCharm/config idea.system.path=G:/software/Pycharm/PyCharm 2021.2.2/configuration_files/.PyCharm/system idea.plugins.path=G:/software/Pycharm/PyCharm 2021.2.2/configuration_files/plugins idea.log.path=G:/software/Pycharm/PyCharm 2021.2.2/configuration_files/log ``` 这允许用户将数据存储到非 C 盘的位置上,从而优化性能并节省系统分区的空间。 #### 在 Python 脚本头部声明工作目录 对于具体的 Python 脚本而言,在某些情况下可能希望脚本能自动识别其所在的工作目录以便于访问资源文件。虽然这不是直接由 PyCharm 提供的功能,但是可以通过编程方式实现这一点[^2]。可以在代码开头加入如下几行来确保当前工作目录指向预期位置: ```python import os os.chdir(os.path.dirname(__file__)) print(f"Working directory set to {os.getcwd()}") ``` 这段代码的作用是改变进程的当前工作目录至该 `.py` 文件所在的目录,并打印确认信息。 #### 将文件夹标记为源码根目录 (Sources Root) 为了使项目内部能按相对路径正确解析依赖关系,应该把项目的根目录设为 Sources Root。具体操作方法是从菜单栏中依次点击 File -> Settings... 或者直接右键单击想要作为起点的那个包名,接着从上下文菜单里选取 Mark Directory as -> Sources Root[^5]。完成这一设定后,所有位于该项目下的模块都可以基于这个新的基线来进行相互引用而无需担心绝对路径带来的麻烦。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值