[英]Xcode: directory file for simulator Iphone
我想在文件“file.txt”中寫一個字符串:我的項目中的這個文件(對於Iphone)在Resources中; 我嘗試在這個文件中寫一個字符串,但它不起作用,我顯示我的代碼。
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寫這個文件,而不是用設備
我不確定您是否能夠在您的軟件包中寫入,但您可以在您的文檔目錄中代替您的代碼。 你為什么不嘗試這個?
使用相同的代碼,您將在以下位置找到您的文件:
/ Users / YOURUSER / Library / Application Support / iPhone Simulator / IOSVERSION / Applications / APPID /Documents/file.txt
這改變了XCode 6和iOS 6,這很棘手。
文檔目錄現在隱藏在:
~/Library/Developer/CoreSimulator/Devices/<some-id>/data/Containers/Data/Application/<some-other-id>
看起來每個應用程序啟動時位置都在變化,因此很難跟蹤。 我從這篇文章中借用了一個簡單的提示,我將為懶惰提供:
#if TARGET_IPHONE_SIMULATOR
// where are you?
NSLog(@"Documents Directory: %@", [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]);
#endif
例如,將它添加到您的applicationDidFinishLaunching
方法中,應該更容易!
如果您想了解更多詳細信息,請查看原始文章
注意:我知道這個問題很老,但是我在搜索時發現了這個問題,所以我認為添加一個更新的答案可能會為我們中的一些人服務!
模擬器中應用程序的Documents目錄位於;
~/Library/Application Support/iPhone Simulator/YOUR-IOS-VERSION/Applications/UNIQUE-KEY-FOR-YOUR-APP/Documents
使用此處的示例代碼,它不會寫入您的Resources文件夾,而是寫入Simulator的Documents文件夾,這實際上是您應該在設備上編寫的文件,但正如您所提到的那樣,您只想在模擬器上執行此操作,你可以使用
filePath = [[NSBundle mainBundle] bundlePath] stringByAppendingPathComnponent:@"Resources"]
但是, 不要在運送應用程序中執行此操作,它將失敗。
將其粘貼在終端中。
open ~/Library/Application\ Support/iPhone\ Simulator/
在gdb中,停止處理並粘貼它,然后打印文檔的位置。 [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]
po [[[NSFileManager defaultManager] URLsForDirectory:9 inDomains:1] lastObject]
如果您使用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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.