iOS文件路径上的/ private前缀表示什么? [英] What does the /private prefix on an iOS file path indicate?
问题描述
当我的应用程序在iPhone上运行时,我有一个错误,但是当它在模拟器上运行时却没有。我使用主目录路径的长度来提取/ Documents中文件的相对路径。不幸的是,这并不总能在iPhone上正常工作,因为前缀/ private正被添加到主路径中。但是,无论有没有前缀,都可以引用相同的文件。以下代码演示了此不一致性。 / private的目的是什么?iOS何时提供?
I have a bug when my app runs on the iPhone but not when it runs on the simulator. I was using the length of the home directory path to extract the relative path of a file in /Documents. Unfortunately this doesn't always work correctly on the iPhone because the prefix "/private" is being added to the home path. However, with or without the prefix, the same file is referenced ok. The following code demonstrates this inconsistency. What is the purpose of "/private" and when is it supplied by 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行之间的差异。
The following is written to the log when running on the iPhone. I manually spaced the output to show the difference between lines 1 and 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):
The following is written to the log when running on the simulator (no "/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 /
另外。
I tried this from the debugger and discovered that URLByResolvingSymlinksInPath
"fixes" the /private/
addition.
(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
是我们真正想要的。
as you can see, file://localhost/var
is what we really want here.
因此,很明显 / private / var
是 / var
的符号链接。
然而,@ Kevin-Ballard指出这不是真的。我确认他是正确的, / var
是 / private / var
(叹气)的符号链接
Because of this, it seemed obvious that /private/var
is a symlink to /var
.
However, @Kevin-Ballard points out that is not true. I confirmed that he is correct, and /var
is the symlink to /private/var
(sigh)
(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
仍然听起来像是一个适用于模拟器和设备的好解决方案,如果发生变化,将来还能继续工作。
So URLByResolvingSymlinksInPath
is doing something funny here, but now we know. For this particular problem, URLByResolvingSymlinksInPath
still sounds like a good solution that works for both the simulator and the device and should continue to work in the future if something changes.
这篇关于iOS文件路径上的/ private前缀表示什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!