[英]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.