2011-04-08 77 views
23

我想在文件「file.txt」中寫入一個字符串:我的項目中的這個文件(用於Iphone)位於Resources中;我嘗試在這個文件中編寫一個字符串,但它不起作用,我顯示了我的代碼。Xcode:用於模擬器的目錄文件Iphone

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"file.txt"]; 
NSError *error; 
[outputString writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error]; 

我想在這個文件與模擬器的Xcode寫,而不是與設備

回答

49

我不知道,如果你能在你的包寫的,但你可以在你的文檔目錄,而不是爲你的代碼確實。你爲什麼不試試這個?

使用相同的代碼,你會發現在你的文件:

/用戶/ YOURUSER /庫/應用程序支持/ iPhone模擬器/ IOSVERSION /應用/ APPID /Documents/file.txt

+0

好吧,它的工作,謝謝.... – CrazyDev 2011-04-08 09:47:13

+0

如何知道APPID? – 2014-10-02 12:24:50

15

模擬器中您的應用程序的Documents目錄位於;

~/Library/Application Support/iPhone Simulator/YOUR-IOS-VERSION/Applications/UNIQUE-KEY-FOR-YOUR-APP/Documents 
2

有了,你在這裏也不會寫入的資源文件夾,但該示例代碼到模擬器,這實際上是對Documents文件夾,你應該在設備上寫,但你提到,你只要做到這一點在模擬器上,你可以使用

filePath = [[NSBundle mainBundle] bundlePath] stringByAppendingPathComnponent:@"Resources"] 

但是,在裝運應用程序做到這一點,它就會失敗。

+0

接口類型不能靜態分配....爲什麼? – CrazyDev 2011-04-08 09:50:35

+0

我不確定你在這裏問什麼 - 什麼接口類型? – 2011-04-08 10:40:56

+0

沒關係,我在filePath之前沒有寫「*」 – CrazyDev 2011-04-08 15:11:20

3

將其粘貼到終端中。

open ~/Library/Application\ Support/iPhone\ Simulator/ 
19

這改變了XCode 6和iOS 6,這很棘手。

的文件目錄現在都隱藏在:

~/Library/Developer/CoreSimulator/Devices/<some-id>/data/Containers/Data/Application/<some-other-id> 

看來位置在每個應用程序啓動是變化的,所以真的很難跟蹤。有一個簡單的提示,我從this article和借來的,我會包括對懶惰的:

#if TARGET_IPHONE_SIMULATOR 
// where are you? 
NSLog(@"Documents Directory: %@", [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]); 
#endif 

例如添加到您的applicationDidFinishLaunching方法,應該更容易!

如果您想了解更多的細節,只是看看到original article

注:我知道這個問題是老了,但很好,我發現它,而搜索,所以我覺得加入一個更新的答案可能成爲一些我們!

0

在gdb中,停止進程並粘貼它,然後打印文檔的位置。 [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]

po [[[NSFileManager defaultManager] URLsForDirectory:9 inDomains:1] lastObject] 
0

如果您正在使用的Xcode 7和更高遵循這樣的:

文件目錄:在您的應用程序和類型的任何地方

file:///Users/codercat/Library/Developer/CoreSimulator/Devices/YourProjectDict/data/Containers/Data/Application/(sample digit)7F53CFB3-C534-443F-B595-62619BA808F2/Documents/your file located here 
8

在斷點處停止 「寶NSHomeDirectory()」在調試器中。使用Finder的Go> Go To Folder功能直接跳到那裏。 how the "po NSHomeDirectory()" command in debugger shows path to current app's home directory