如何在 Cocoa 或 Xcode 编程中使用 mainmenu nib (xib) 文件设置环境变量?

发布于 2024-12-22 17:34:48 字数 744 浏览 2 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

枫以 2024-12-29 17:34:48

我想知道如何构建 nib(或 xib)文件,例如,
MainMenu.xib,在 Xcode 3 的最新版本中,以便它可以用于
设置环境。

Nib 文件与环境变量没有任何关系。它们不包含任何代码,除了提供用于实例化应用程序代码提供的类的数据之外,它们对程序没有任何影响。我想您可以编写一个设置环境变量的类,然后使用 nib 实例化该类,但是这样做与仅在代码中实例化该类没有区别。

我所做的一件事是在不使用笔尖的情况下编写 setenv 代码
file:...但是,当我执行 Cocoa GUI 时这不起作用
通过双击 xxx.app 进行编程,尽管当我这样做时这有效
执行程序的命令行版本。

你怎么知道它没起作用?您是否正在调用 getenv() 来检查代码中 R_HOME 的值,或者您是否在终端中使用类似 env 的命令?如果是后者,您所看到的不是设置变量的同一环境。

所以,我似乎需要一种方法来设置环境变量
Mac GUI 应用程序启动。

你想实现什么目标?您似乎不太可能为自己的程序使用设置一个环境变量——它已经知道该值,因此退出环境似乎毫无意义。您是否正在尝试与另一个程序进行通信?

我见过其他设置环境变量的方法,但我想要
知道如何使用 nib(或 xib)文件设置环境变量,
并使用 NSBundle 的 loadNibNamed 方法加载它。

同样,nib 文件和环境变量之间确实没有交集。如果您希望能够根据某些外部变量修改应用程序的行为,则可以在 .login 文件中设置环境变量,并且您的应用程序应该可以读取这些值。也许更好的解决方案是使用默认系统——您的应用程序可以通过 NSUserDefaults 读取和写入默认系统中的值,并且您可以使用 defaults 在命令行读取和写入相同的值命令。

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.

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.

One thing that I did was to code setenv without using a nib
file:...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.

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.

So, it seems like that I need a way to set environment variables when
a Mac GUI application is launched.

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?

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.

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.

初雪 2024-12-29 17:34:48

环境变量通常与方案中的选项一起设置。

从“产品”菜单中选择“编辑方案”并选择您所需的目标。现在,在右侧选择“参数”选项卡。

在“参数”选项卡上,您可以设置要传递到正在运行的应用程序的命令行参数和环境变量。

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.

眼眸印温柔 2024-12-29 17:34:48

要在可分发应用程序中设置环境变量,以便甚至为用户设置它们(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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文