宏还可以生成方法名称吗? [英] Can a macro also generate a method name?

查看:87
本文介绍了宏还可以生成方法名称吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用宏来创建方法名通常是否有效?我的意思是……实际上,这只是在编译器实际运行之前进行简单的文本替换,对吗?

Is it generally valid that a macro is used to create a method name? I mean...actually it's just simple text replacement before the compiler actually runs, right?

推荐答案

是的,它是有效的;宏扩展发生在编译器甚至没有读取代码之前.主要限制是不能将预处理器指令嵌入到预处理器指令中.因此,例如:

Yes, it is valid; macro expansion occurs before the compiler even reads the code. The main limitation is that one cannot embed a preprocessor directive within a preprocessor directive. So, for example:

// This is ok:
#define PREFIX(X) this_name_is_prefixed_ ## X
// ...
- (void) PREFIX(doSomething):id;
// ...

// But this isn't:
#define IMPORT(X) #import X
IMPORT(<Foundation/Foundation.h>) // <= Don't expect this to work

除"#import"指令外,Objective-C的预处理器基本上与 C预处理器. ("#import"类似于"#include",除了#import意味着仅包含一次,因此仅包含在#import中的标头不需要预处理器保护).

With the exception of the "#import" directive, Objective-C's preprocessor is basically the same as the C preprocessor. (The "#import" is like "#include", except that #import implies include only once, so preprocessor guards are not required for headers that are included only with #import).

这篇关于宏还可以生成方法名称吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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