Xcode:模拟器Iphone的目录文件

时间:2011-04-08 09:21:32

标签: iphone xcode string directory writetofile

我想在文件“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在此文件中写入,而不是使用设备

8 个答案:

答案 0 :(得分:49)

我不确定您是否能够在您的软件包中写入,但您可以在您的文档目录中代替您的代码。你为什么不尝试这个?

使用相同的代码,您将在以下位置找到您的文件:

/ Users / YOURUSER /图书馆/应用支持/ iPhone模拟器/ IOSVERSION /应用程序/ APPID /Documents/file.txt

答案 1 :(得分: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

注意:我知道问题已经过时了,但是我在搜索时发现了这个问题,所以我认为添加更新的答案可能会为我们中的一些人服务!

答案 2 :(得分:15)

模拟器中应用的Documents目录位于;

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

答案 3 :(得分:8)

在应用程序的任何位置停止断点并在调试器中键入“po NSHomeDirectory()”。使用Finder的Go&gt;转到文件夹功能直接跳转到那里。 how the "po NSHomeDirectory()" command in debugger shows path to current app's home directory

答案 4 :(得分:2)

使用此处的示例代码,它不会写入您的Resources文件夹,而是写入Simulator的Documents文件夹,这实际上是您应该在设备上写的地方,但正如您所提到的那样,您只想这样做在模拟器上,你可以使用

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

但是,在发货应用中执行此操作,它将会失败。

答案 5 :(得分:1)

将其粘贴在终端中。

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

答案 6 :(得分:0)

在gdb中,停止处理并粘贴它,然后打印文档的位置。 [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]

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

答案 7 :(得分: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