iphone - iOS 文件路径上的/private 前缀表示什么?

标签 iphone ios filepath

我的应用程序在 iPhone 上运行时出现错误,但在模拟器上运行时却没有。我使用主目录路径的长度来提取/Documents 中文件的相对路径。不幸的是,这并不总是在 iPhone 上正常工作,因为前缀“/private”被添加到主路径。但是,无论有无前缀,都可以引用相同的文件。下面的代码演示了这种不一致。 “/private”的用途是什么?iOS 何时提供它?

- (IBAction)testHomepath:(id)sender {
    NSFileManager *fmgr = [NSFileManager defaultManager];
    NSString  *homePath = [NSString stringWithFormat:@"%@/Documents",NSHomeDirectory()];
    NSString  *dirPath  = [homePath stringByAppendingPathComponent:@"TempDir"];
    NSURL     *dirURL   = [NSURL fileURLWithPath:dirPath];
    NSString  *filePath = [dirPath  stringByAppendingPathComponent:@"test.jpg"];
    [fmgr createDirectoryAtPath:dirPath withIntermediateDirectories:NO attributes:nil error:nil];
    [fmgr createFileAtPath:filePath contents:nil attributes:nil];
    NSArray *keys  = [[NSArray alloc] initWithObjects:NSURLNameKey,nil];
    NSArray *files = [fmgr contentsOfDirectoryAtURL:dirURL includingPropertiesForKeys:keys options:0 error:nil];
    NSURL *f1 = (files.count>0)? [files objectAtIndex:0] : 0;
    NSURL *f2 = (files.count>1)? [files objectAtIndex:1] : 0;
    bool   b0 = [fmgr fileExistsAtPath:filePath];
    bool   b1 = [fmgr fileExistsAtPath:f1.path];
    bool   b2 = [fmgr fileExistsAtPath:f2.path];

    NSLog(@"File exists=%d at path:%@",b0,filePath);
    NSLog(@"File exists=%d at path:%@",b1,f1.path);
    NSLog(@"File exists=%d at path:%@",b2,f2.path);
}

在iPhone上运行时写入日志如下。我手动分隔输出以显示第 1 行和第 2 行之间的差异。

2013-02-20 16:31:26.615 Test1[4059:907] File exists=1 at path:        /var/mobile/Applications/558B5D82-ACEB-457D-8A70-E6E00DB3A484/Documents/TempDir/test.jpg
2013-02-20 16:31:26.622 Test1[4059:907] File exists=1 at path:/private/var/mobile/Applications/558B5D82-ACEB-457D-8A70-E6E00DB3A484/Documents/TempDir/test.jpg
2013-02-20 16:31:26.628 Test1[4059:907] File exists=0 at path:(null)

在模拟器上运行时(无“/private”)写入日志如下:

2013-02-20 16:50:38.730 Test1[7224:c07] File exists=1 at path:/Users/kenm/Library/Application Support/iPhone Simulator/6.1/Applications/C6FDE177-958C-4BF5-8770-A4D3FBD281F1/Documents/TempDir/test.jpg
2013-02-20 16:50:38.732 Test1[7224:c07] File exists=1 at path:/Users/kenm/Library/Application Support/iPhone Simulator/6.1/Applications/C6FDE177-958C-4BF5-8770-A4D3FBD281F1/Documents/TempDir/.DS_Store
2013-02-20 16:50:38.733 Test1[7224:c07] File exists=1 at path:/Users/kenm/Library/Application Support/iPhone Simulator/6.1/Applications/C6FDE177-958C-4BF5-8770-A4D3FBD281F1/Documents/TempDir/test.jpg

最佳答案

我在调试器中进行了尝试,发现 URLByResolvingSymlinksInPath“修复”了 /private/ 添加。

(lldb) p (NSURL *)[NSURL fileURLWithPath:@"/private/var" isDirectory:YES]
(NSURL *) $1 = 0x1fd9fc20 @"file://localhost/private/var/"
(lldb) po [$1 URLByResolvingSymlinksInPath]
$2 = 0x1fda0190 file://localhost/var/

(lldb) p (NSURL *)[NSURL fileURLWithPath:@"/var" isDirectory:YES]
(NSURL *) $7 = 0x1fd9fee0 @"file://localhost/var/"
(lldb) po [$7 URLByResolvingSymlinksInPath]
$8 = 0x1fda2f50 file://localhost/var/

如您所见,file://localhost/var 是我们真正想要的。

因此,/private/var 显然是 /var 的符号链接(symbolic link)。 但是,@Kevin-Ballard 指出事实并非如此。我确认他是正确的,/var/private/var 的符号链接(symbolic link)(叹息)

(lldb) p (NSDictionary *)[[NSFileManager defaultManager] attributesOfItemAtPath:@"/var" error:nil]
(NSDictionary *) $3 = 0x1fda11b0 13 key/value pairs
(lldb) po $3
$3 = 0x1fda11b0 {
    ...
    NSFileType = NSFileTypeSymbolicLink;
}

(lldb) p (NSDictionary *)[[NSFileManager defaultManager]   attributesOfItemAtPath:@"/private/var" error:nil]
(NSDictionary *) $5 = 0x1fda4820 14 key/value pairs
(lldb) po $5
$5 = 0x1fda4820 {
    ...
    NSFileType = NSFileTypeDirectory;
}

所以 URLByResolvingSymlinksInPath 在这里做了一些有趣的事情,但现在我们知道了。对于这个特定问题,URLByResolvingSymlinksInPath 听起来仍然是一个很好的解决方案,适用于模拟器和设备,并且在将来发生变化时应该会继续工作。

关于iphone - iOS 文件路径上的/private 前缀表示什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14992834/

相关文章:

iphone - 用核心数据实现 "Did you mean?"

iphone - 如何在 iPhone 中生成二维码?

ios - 从 Core Data 中检索单个对象

从列表中设置路径的Pythonic方式

iphone - 使用系统图标或图像图标的自定义导航栏按钮

ios - 如何在 iOS 8/Swift 中处理带有屏幕尺寸/方向变化的 NSLayoutConstraints?

ios - 使用 iOS UIWebView 时的 Rails 布局(application.html.erb)逻辑

windows -\\?\是什么意思,放在文件路径前

java - Bufferedreader 和文件路径 - 简单

iphone - 在 float 和 float 之间获取随机 float 的最佳方法是什么?