applicationDocumentsDirectory Path 与 URL

本文介绍了在iOS开发中如何通过两种不同的方法来获取应用程序的文档目录路径,并对比了使用NSURL和NSString的不同之处,强调了在文件系统操作时应选择正确的路径获取方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

-(NSURL*)applicationDocumentsDirectory
{
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
-(NSString*)applicationDocumentsDirectoryPath
{
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}

区别如下:

NSLog(@"%@",storeURL);
NSLog(@"%@",storePath);
NSLog(@"%@",[storeURL absoluteString]);

结果分别为

file://localhost/Users/apple/Library/Application%20Support/iPhone%20Simulator/6.0/Applications/A2187B83-2937-46AC-9829-D87B4D292E7C/Documents/

/Users/apple/Library/Application Support/iPhone Simulator/6.0/Applications/A2187B83-2937-46AC-9829-D87B4D292E7C/Documents/

file://localhost/Users/apple/Library/Application%20Support/iPhone%20Simulator/6.0/Applications/A2187B83-2937-46AC-9829-D87B4D292E7C/Documents/

因此若需要使用方法

NSFileManager* fileManager = [NSFileManager defaultManager];
[fileManager fileExistsAtPath:storePath];

必须使用applicationDocumentsDirectoryPath,而不能使用 [URL absoluteString];

 

转载于:https://www.cnblogs.com/wxy325/archive/2013/01/31/2886575.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值