问题
I need a way to intercept the code before it goes into the compiler, change it, AND have it remain the same in the file / in Xcode afterwards.
I thought build scripts were a sort of stream interception but that doesn't seem to be the case. Another method might be to run a script both before and after build. Are there any implications with this I should be aware of?
----- EDIT -----
Why? I have an idea for an auto-logging system based on a certain comment syntax. I want to be able, on build, to parse a certain string and replace it with a logging function but have the code remain unchanged. Is it possible?
----- UPDATE -----
It seems a custom compiler might be the way forward, or at least a plugin that wraps current LLVM clang. I've been investigating this. Here's a related question for those interested: Xcode custom compiler which wraps and does a passthru to clang
回答1:
If by "on build" you mean before actually compiling the code, that means you need to open Xcode every time you want to run your script. I don't believe there is a way to do this. However, you CAN use Automator to receive a string before running the code, but you would need to use Automator to build the app, which is limiting. Another method would be to make 2 classes in your app: a pre-run class and a main class. The pre-run class will prompt the user or read a file (or something like that) to get the string, and run the main class in a special way depending on the string.
Edit: response to comment
I don't know how to do this in Objective-C, but you can always rename your main file to main.mm (.mm files contain both C++ and Objective-C code) and add C++ code to it. However, this is using the Console:
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];
}
I'm not on a PC now so I can't check code, but you can search for how to read and write to files in C++ and Obj-C. The only hint I can give right now is that you need #include <iostream> at the beginning if you use C++ to read/write the file.
If you don't want to use the console (which is normal if you wan to publish this) you can also make a dialog. In this example, you don't need C++ so you don't rename the main file. Make a new window (referred to as theDialog from now on) that is visible at start and make sure that your main graphical interface does NOT show up startup. Add a text field to theDialog and give it a name (in this example, tf). Add a label to tell the user what to put in the text field. Add a button and link it to an action. In this action, put the following code:
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];
}
来源:https://stackoverflow.com/questions/19068830/xcode-is-it-possible-to-intercept-and-change-code-on-build-without-updating-the