如何在 Cocoa 或 Xcode 编程中使用 mainmenu nib (xib) 文件设置环境变量?
我对 Cocoa 编程和 Xcode 都很陌生。我想知道如何使用 MainMenu.nib (或 .xib)文件设置环境(或设置环境变量)。我在主函数中有别人的代码,如下所示:
[NSApplication sharedApplication];
[NSBundle loadNibNamed:@"MainMenu" owner: NSApp];
在第二行之后,它可以获得一个环境变量:
if (!getenv("R_HOME")) {
fprintf(stderr, "R_HOME is not set.\n");
return -1;
}
我想知道如何在 Xcode 3 的最新版本中构造一个 nib(或 xib)文件,例如 MainMenu.xib版本,以便可以使用它来设置环境。我做的一件事是在不使用 nib 文件的情况下编写 setenv 代码:
setenv("R_HOME", "/Library/Frameworks/R.framework/Resources", 1)
但是,当我通过双击 xxx.app 执行 Cocoa GUI 编程时,这不起作用,尽管当我执行程序的命令行版本时,这不起作用。所以,我似乎需要一种在 Mac GUI 应用程序启动时设置环境变量的方法。我已经看到了设置环境变量的其他方法,但我想知道如何使用 nib (或 xib)文件设置环境变量,并使用 NSBundle 的 loadNibNamed 方法加载它。
I am new to both Cocoa programming and Xcode. I am wondering how I can setenv (or set environment variable) using MainMenu.nib (or .xib) file. I have someone else's code in the main function like this:
[NSApplication sharedApplication];
[NSBundle loadNibNamed:@"MainMenu" owner: NSApp];
After the second line, it can get an environment variable:
if (!getenv("R_HOME")) {
fprintf(stderr, "R_HOME is not set.\n");
return -1;
}
I want to know how one can construct a nib(or xib) file, e.g., MainMenu.xib, in Xcode 3's latest version so that it can be used to setenv. One thing that I did was to code setenv without using a nib file:
setenv("R_HOME", "/Library/Frameworks/R.framework/Resources", 1)
But, this did not work when I execute the Cocoa GUI programming by double clicking the xxx.app although that worked when I execute the program's command line version. So, it seems like that I need a way to set environment variables when a Mac GUI application is launched. I have seen other ways of setting environment variables, but I want to know how one can set environment variables using nib (or xib) files, and by loading it using NSBundle's loadNibNamed method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Nib 文件与环境变量没有任何关系。它们不包含任何代码,除了提供用于实例化应用程序代码提供的类的数据之外,它们对程序没有任何影响。我想您可以编写一个设置环境变量的类,然后使用 nib 实例化该类,但是这样做与仅在代码中实例化该类没有区别。
你怎么知道它没起作用?您是否正在调用 getenv() 来检查代码中 R_HOME 的值,或者您是否在终端中使用类似
env
的命令?如果是后者,您所看到的不是设置变量的同一环境。你想实现什么目标?您似乎不太可能为自己的程序使用设置一个环境变量——它已经知道该值,因此退出环境似乎毫无意义。您是否正在尝试与另一个程序进行通信?
同样,nib 文件和环境变量之间确实没有交集。如果您希望能够根据某些外部变量修改应用程序的行为,则可以在 .login 文件中设置环境变量,并且您的应用程序应该可以读取这些值。也许更好的解决方案是使用默认系统——您的应用程序可以通过 NSUserDefaults 读取和写入默认系统中的值,并且您可以使用
defaults
在命令行读取和写入相同的值命令。Nib files don't have anything to do with environment variables. They don't contain any code, and they have no impact on the program other than supplying data used to instantiate classes provided by the application's code. I suppose you could write a class that sets environment variables and then use a nib to instantiate that class, but there'd be no difference between doing that and just instantiating the class in your code.
How do you know it didn't work? Are you calling getenv() to check the value of R_HOME in your code, or are you using a command like
env
in the terminal? If the latter, you're not looking at the same environment where the variable was set.What are you trying to accomplish? It seems unlikely that you'd be setting an environment variable for your own program's use -- it already knows that value, so going out the environment seems pointless. Are you trying to communicate with another program?
Again, there's really no intersection between nib files and environment variables. If you want to be able to modify your app's behavior based on some external variable, you can set environment variables in your .login file, and those values should be readable by your app. Perhaps a better solution would be to use the defaults system -- your app can read and write values in the defaults system via NSUserDefaults, and you can read and write those same values at the command line using the
defaults
command.环境变量通常与方案中的选项一起设置。
从“产品”菜单中选择“编辑方案”并选择您所需的目标。现在,在右侧选择“参数”选项卡。
在“参数”选项卡上,您可以设置要传递到正在运行的应用程序的命令行参数和环境变量。
Environment variables are usually set along with options in your schemes.
From the "Product" menu choose "Edit scheme" and select your desired target. Now on the right hand side choose the "Arguments" tab.
On the "Arguments" tab you can set command line parameters and environment variables to be passed along to the running application.
要在可分发应用程序中设置环境变量,以便甚至为用户设置它们(Xcode 仅影响您的调试运行),请设置 应用的 Info.plist 中的
LSEnvironment
属性。To set environment variables in your distributable application, such that they will be set even for users (Xcode only affects your debugging runs), set the
LSEnvironment
property in your app's Info.plist.