是否可以在 Objective-C 中创建自定义指令? [英] Is it possible to create custom directives in Objective-C?

查看:71
本文介绍了是否可以在 Objective-C 中创建自定义指令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Objective-C 有如下指令:

Objective-C has directives like:

  • @interface
  • @实现
  • @end
  • @protocol
  • @property
  • @synthesize

我想到了诸如复杂的 marco 或代码生成器之类的东西.是否可以为代码生成目的创建自定义指令?一种可能的用途是为 CoreData 生成方法.

I think of these things like sophisticated marco or code-generators. Is it possible to create custom directives for code-generation purposes? One possible use is generating methods for CoreData.

我不这么认为,因为我从未见过它,但我的世界不是世界.

I'm thinking not, because I've never seen anything about it, but my world isn't the world.

后续问题:

Jonathan 在下面提到可以编写自己的预处理器,这引出了如何做的问题.目前,#define SYMBOLIC_CONSTANT 102 将在文件移动到编译器之前用文件中的字符 102 替换字符 SYMBOLIC_CONSTANT 的所有实例.

Jonathan mentioned below that it is possible to write your own preprocessor and this begs the question of how. Currently, #define SYMBOLIC_CONSTANT 102 will replace all instances of the characters SYMBOLIC_CONSTANT with the characters 102 in the file before the files moves on to the compiler.

我知道 XCode 您可以向 Targets 构建过程添加运行脚本阶段".因此,我可以编写一个脚本来查找我的自定义预处理指令,例如$coredata",然后让脚本生成一个新文件,该文件将 $coredata 字符替换为某些代码字符.但是根据我对 XCode 构建过程的理解,您无法将更改的文件提供给编译器源阶段.文件由 IDE 指定并锁定.

I know it XCode you can add a "Run Script Phase" to a Targets build process. So I could write a script to find my custom preprocess directives like '$coredata' and then have the script generate a new file that with the characters $coredata replaced with some characters of code. But from what I understand of XCode's build process you can't feed altered files into the Compiler Sources phase. The files are specified and locked by the IDE.

有没有人做过类似的事情?我知道使用外部构建系统是可能的,但老实说,我没有达到那种理解水平.我不知道构建和运行"按钮的技术细节.

Has anyone done something similar? I know it's possible with external build system, but to be honest I'm not at that level of understanding. I don't know the technical details of what the Build and Run button does.

与此同时,我将开始阅读 Apple 的 XCode 文档...

In the meantime, I'll start reading Apple's XCode Documentation...

感谢您的回复!

推荐答案

您的想法是正确的:在您的代码中不可能做到这一点.添加更多 @ 指令的唯一方法是通过编译器本身.即使您遇到了所有这些麻烦,我几乎可以保证对它们的语法高亮支持已硬编码到某个 Xcode 配置文件中.

Your thinking is correct: it is impossible to do this in your code. The only way to add more @-directives is via the compiler itself. Even if you went to all that trouble, I can almost guarantee that the syntax highlighting support for them is hard-coded into an Xcode configuration file somewhere.

哦,如果您正在考虑使用预处理器宏,我的理解是 @ 字符在预处理器宏中是非法的.

Oh, and if you were considering the use a pre-processor macro, it is my understanding that the @ character is illegal in pre-processor macros.

我进行了测试,我是正确的.在 C 预处理器宏中使用 @ 字符是非法的.它们遵循与变量名称相同的规则.

I ran a test, and I am correct. Using the @ character in a C preprocessor macro is illegal. They follow the same rule as variable names.

这篇关于是否可以在 Objective-C 中创建自定义指令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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