Objective-C 和 Xcode 中的工作目录:调试模式与可执行文件
我正在使用 Xcode 用 Objective-C 编写一个程序。我的程序创建一个文件如下:
[@"" writeToFile:fileName atomically:YES encoding:NSUTF8StringEncoding error:NULL];
我希望在与可执行文件相同的目录中创建该文件。当我从 Xcode 运行该程序时,该文件会按预期在调试目录中创建。
但是,当我运行 .app 文件时,该文件会在根目录中创建。如何让程序在 .app 文件所在的目录中创建文件。
多谢。
编辑:这是一个 MacOS 应用程序
编辑2:嗯,看来我不应该写入 .app 目录。谢谢 bbum 和 Paul R。正确的方法是什么?更具体地说,这就是我正在做的事情:每次用户单击应用程序中的按钮时,连接到串行端口的硬件都会发送一堆数据,这些数据将写入新文件。在应用程序运行时,这种情况可能会发生多次,因此可能会创建大量文件。我希望它们全部创建在同一个文件夹中。
I am writing a program in Objective-C using Xcode. My program creates a file as follows:
[@"" writeToFile:fileName atomically:YES encoding:NSUTF8StringEncoding error:NULL];
I would like the file to be created in the same directory as the executable. When I run the program from Xcode, the file is created in the debug directory as expected.
However, when I run the .app file, the file is created in the root directory. How can I get the program to create a file in the directory where the .app file is located.
Thanks a lot.
EDIT: This is a MacOS application
EDIT2: Well, it seems that I shouldn't be writing to the .app directory. Thanks bbum and Paul R. What is the proper way to do it? To be more concrete, here's what I am doing: each time the user clicks a button in the application, a piece of hardware connected to a serial port will send a bunch data which will be written to a new file. This can happen any number of times while the application is running, so numerous files may be created. I would like them all created in the same folder.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您绝对不能对应用程序的初始工作目录做出任何假设,因为这取决于启动它的方法(例如 Finder、终端(通过
open
)) 、Xcode、gdb、第三方实用程序等)。您应该使用适当的 API 来查找合适的目录来存储临时文件或用户特定文件或您需要执行的任何操作。它永远不应该位于应用程序的捆绑包中,也不应该位于相对于初始工作目录的路径中。You must never make any assumptions about the initial working directory for your application, as this will depend on what method was used to launch it (e.g. Finder, Terminal (via
open
), Xcode, gdb, third party utility, etc). You should use an appropriate API to find a suitable directory to store temporary files or user-specific files or whatever it is you need to do. This should never be within the app's bundle and never at a path that is relative to the initial working directory.您不希望在
.app
包装器内创建该文件。这永远都不是正确的答案;您的应用程序可能很容易安装在当前用户没有YourApp.app
包装器的写入权限的地方。(例如,我的主用户帐户是非管理员帐户,并且所有应用程序都安装为仅管理员写入。如果应用程序因无法写入其应用程序包装器而无法工作,则该应用程序将被放入垃圾箱。)
< a href="https://stackoverflow.com/questions/5007073/noob-where-can-i-store-my-applications-files/5007143#5007143">请参阅此问题以了解文件位置的概述应保存。取决于文件的作用。
You do not want the file to be created inside the
.app
wrapper. That is never the right answer; your application may easily be installed somewhere where the current user does not have write access to theYourApp.app
wrapper.(For example, my main user account is non-admin and all applications are installed admin-write-only. If an app ever fails to work because it can't write to its app wrapper, the app goes in the trash.)
See this question for an outline of where files should be stored. Depends on the role of the file.