8

私が試してみました:

path = @"~/Desktop/files/text.plist";
NSDictionary *aDict = [NSDictionary dictionaryWithContentsOfFile:path];

結果パス: ~/Desktop/files/text.plist

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

結果パス: $(HOME)/Desktop/files/text.plist

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

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

  1. XCode でユーザーのデスクトップ パスにアクセスする方法はありますか?
  2. 上記の例で環境変数を使用できますか?また、その方法は?

編集: 2 番目の質問の詳細: MAC コンソールに環境変数 (またはマクロ) があるように、これらをコード内で使用することはできますか? それらが使用できる場合、誰かが例を手伝ってくれますか? $HOME_DIRECTORY など。

4

4 に答える 4

2

これは機能するはずです:

NSString *pathToDesktop = [NSString stringWithFormat:@"/Users/%@/Desktop/text.txt", NSUserName()];
于 2012-06-13T01:47:06.087 に答える
1

このステートメントを使用すると、デスクトップ フォルダーが通常の場所にあると想定されます。

[NSURL fileURLWithPath:[NSHomeDirectory()stringByAppendingPathComponent:@"Desktop"];
于 2016-03-06T13:43:10.737 に答える
-3

これはあなたの#1の質問に対してそれを行うはずです:

 NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 NSString* documentsDirectoryPath = [paths objectAtIndex:0];

2 番目の質問について詳しく説明していただけますか。:)

于 2012-06-12T23:18:03.743 に答える