如何在XCode中获得用户桌面的路径?

iOS*_*com 7 cocoa path environment-variables ios

我试过了:

path = @"~/Desktop/files/text.plist";
NSDictionary *aDict = [NSDictionary dictionaryWithContentsOfFile:path];
Run Code Online (Sandbox Code Playgroud)

ResultPath:〜/ Desktop/files/text.plist

path = @"$(HOME)/Desktop/files/text.plist";
NSDictionary *aDict = [NSDictionary dictionaryWithContentsOfFile:path];
Run Code Online (Sandbox Code Playgroud)

ResultPath:$(HOME)/Desktop/files/text.plist

path = [NSHomeDirectory() stringByAppendingString:@"/Desktop/files/text.plist"];
NSDictionary *aDict = [NSDictionary dictionaryWithContentsOfFile:path];
Run Code Online (Sandbox Code Playgroud)

ResultPath:/ Users/my_name/Library/Application Support/iPhone Simulator/5.1/Applications/639DC66A-7092-4ECB-9E48-59935AC1C394/Desktop/files/text.plist

  1. 有没有办法在XCode中访问用户的桌面路径?
  2. 可以在上面的示例中使用环境变量以及如何使用?

编辑:第二个问题的进一步说明:就像MAC控制台中有环境变量(或宏)一样,这些也可以在代码中使用吗?如果可以使用它们,任何人都可以帮助一个例子吗?比如$ HOME_DIRECTORY或类似的东西.

Abi*_*ern 22

您可以在搜索路径中指定桌面:

NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDesktopDirectory, NSUserDomainMask, YES);
NSString * desktopPath = [paths objectAtIndex:0];
Run Code Online (Sandbox Code Playgroud)

这样做的好处是它不会假设用户如何配置他的Mac,如果他的桌面文件夹不在默认位置,这仍然会找到正确的位置.