简体   繁体   English

从命令行运行xcodebuild时,如何写入iPhone SDK中的当前目录?

[英]How can I write to the current directory in the iPhone SDK when running xcodebuild from the command line?

Does anyone know how to obtain the path to the current directory when running xcodebuild (mainly, the project directory) and write files to it? 有谁知道在运行xcodebuild(主要是项目目录)并将文件写入其中时如何获取当前目录的路径? I'm trying to save the results of some unit tests to disk from my obj-c code, but I'd rather not dump them deep in the app's document path if possible. 我正在尝试将某些单元测试的结果从obj-c代码保存到磁盘,但如果可能的话,我宁愿不要将其转储到应用程序的文档路径中。 I might be able to append '../../..' etc. to the document path, but I was hoping there might be a more elegant method. 我可能可以将'../../ ..'等附加到文档路径,但是我希望可以有一种更优雅的方法。

*This is relevant only to the simulator, of course... but perhaps even the simulator attempts to maintain the iPhone file system sandbox? *当然,这仅与模拟器有关……但是,也许模拟器甚至尝试维护iPhone文件系统沙箱? I've had no luck writing to other random locations on my Mac, so I suspect that might be the case. 我在Mac上向其他随机位置进行写操作没有运气,因此我怀疑可能是这种情况。

Thanks for your input! 感谢您的输入!

I figured this out - the answer is embarrassingly simple. 我想通了-答案很简单。 With an emphasis on "embarrassing". 着重于“尴尬”。

If you use something like NSString's writeToFile methods, all you need to do is think of your path as relative to wherever you're running xcodebuild from - no NSSearchPathForDirectoriesInDomains needed. 如果您使用类似NSString的writeToFile方法的方法,那么您所要做的就是将您的路径视为相对于您从何处运行xcodebuild的位置-不需要NSSearchPathForDirectoriesInDomains。

Example: 例:

(if running in ~/src/awesomeapp) (如果在〜/ src / awesomeapp中运行)

[myString writeToFile:@"awesome.file" atomically:YES encoding:NSUTF8StringEncoding error:&error];

Will output to: ~/src/awesomeapp/awesome.file 将输出到:〜/ src / awesomeapp / awesome.file

Yes, I am an idiot. 是的,我是个白痴。 I actually tried this first , but I was attempting to write to a relative directory that didn't exist, thus resulting in an unhelpful "Cannot do this" style error that led me all around the internets and eventually to stackoverflow... (you need to use FileManager#createDirectoryAtPath! There is no automatic path creation, unfortunately.) 我实际上首先尝试过此方法,但是我试图写入一个不存在的相对目录,从而导致无用的“无法执行此操作”样式错误,导致我四处上网,最终导致stackoverflow ...(您需要使用FileManager#createDirectoryAtPath!不幸的是,没有自动创建路径。)

FYI, as should be apparent, you are free to write files wherever you wish on your hard drive when running via xcodebuild - no sandboxing occurs. 仅供参考,很明显,通过xcodebuild运行时,您可以随意在硬盘上的任何位置写入文件-不会出现沙箱。 If you run in the simulator, though, it might be a different story. 但是,如果您在模拟器中运行,则情况可能会不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM