2011-04-08 72 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