如果“on build”是指在实际编译代码之前,则意味着每次要运行脚本时都需要打开 Xcode。我不相信有办法做到这一点。但是,您可以在运行代码之前使用 Automator 接收字符串,但是您需要使用 Automator 来构建应用程序,这是有限制的。另一种方法是在您的应用程序中创建 2 个类:一个预运行类和一个主类。预运行类将提示用户或读取文件(或类似的东西)来获取字符串,并根据字符串以特殊方式运行主类。
编辑:回复评论
我不知道如何在 Objective-C 中执行此操作,但您始终可以将主文件重命名为 main.mm(.mm 文件包含 C++ 和 Objective-C 代码)并向其中添加 C++ 代码。但是,这是使用控制台:
using namespace std // all (this line is optional)
string s = ""; // this
cin >> s; // is C++ (if you omitted the optional line above, the correct code is std::cin >> s;
MainClass mc = //constructor for your "main class" here in Objective-C
if(s == someCertainString){ //hypothetical string and C++ condition checking for a C++ string
[mc doThisACertainWay:];
}else if(s == someOtherString){ // again, hypothetical string that you need to declare
[mc doThisADifferentWay];
}
我现在不在 PC 上,所以无法检查代码,但您可以搜索如何在 C++ 和 Obj-C 中读取和写入文件。我现在可以给出的唯一提示是,#include <iostream>
如果您使用 C++ 读取/写入文件,您需要在开始时使用。
如果您不想使用控制台(如果您想发布它是正常的),您也可以创建一个对话框。在此示例中,您不需要 C++,因此您无需重命名主文件。创建一个在启动时可见的新窗口(从现在开始称为theDialog
),并确保您的主图形界面不显示启动。添加一个文本字段theDialog
并为其命名(在本例中为tf
)。添加一个标签来告诉用户在文本字段中放置什么。添加一个按钮并将其链接到一个操作。在此操作中,输入以下代码:
MainClass mc = // constructor here
NSString *str = [self.tf stringValue];
if([str equalsString:someString]){ // hypothetical string and possible error in the condition checking, I'm new to Obj-C
[mc doThisInACertainWay];
} else {
[mc doThisInADifferentWay];
}