X code,是否有可能拦截和更改code上构建,而无需更新文件?如果是这样,怎么样? [英] Xcode, is it possible to intercept and change code on build without updating the file? If so, how?

查看:213
本文介绍了X code,是否有可能拦截和更改code上构建,而无需更新文件?如果是这样,怎么样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种方式,它进入编译器之前拦截code,改变它,并有/它保持相同的文件X型code之后。

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?

----- -----编辑

为什么呢?我有基于某个注释语法自动记录系统的想法。我希望能够上构建,解析某些字符串和记录功能取代它,但有code保持不变。这可能吗?

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?

----- -----更新

这似乎是一个自定义的编译器可能是向前迈出的一个封装当前LLVM铛的方式,或者至少一个插件。我一直在调查这个。下面是对于那些有兴趣一个相关的问题: X code自定义编译它包装并做了中继,以铿锵

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

推荐答案

如果通过关于构建你居然编译code之前的意思是,这意味着你需要打开X code您想每次运行脚本。我不相信有办法做到这一点。但是,你可以使用的Automator运行code之前收到字符串,但您需要使用的Automator来构建应用程序,这是限制。另一种方法是使2班在您的应用程序:一个pre-运行类和主类。在pre-Run类将提示用户或读取一个文件(或类似的东西)来获取字符串,并根据不同的字符串以特殊的方式运行的主类。

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.

编辑:回应置评

我不知道如何在Objective-C这样做,但你总是可以重命名你的主文件main.mm(.mm文件同时包含C ++和Objective-C code),并添加C ++ code到它。然而,这是使用控制台

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];
}

我不是在PC上,所以我现在不能检查code,但您可以搜索如何读取和写入文件在C ++和的OBJ-C。唯一的提示我现在能给的是,你需要的#include< iostream的方式> 开头,如果你使用C ++读/写文件

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.

如果你不想(如果你婉发布此这是正常的),使用控制台,你也可以做一个对话框。在这个例子中,你不需要C ++,这样你就不能重命名主文件。创建一个新的窗口(从现在起被称为 theDialog ),它是在启动可见光和确保您的主图形界面不显示启动。文本字段添加到 theDialog 并给它一个名称(在这个例子中, TF )。添加一个标签来告诉用户将在文本字段的内容。添加一个按钮,并将其链接到一个动作。在这次行动中,把下面的code:

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];
}

这篇关于X code,是否有可能拦截和更改code上构建,而无需更新文件?如果是这样,怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆