【问题标题】:How to write multiple instruction in Objective-C macro?如何在 Objective-C 宏中编写多条指令?
【发布时间】:2018-10-08 10:29:49
【问题描述】:

我想知道如何在 Objective C 宏中编写多条指令。 例如,我想创建一个运行以下两条指令的宏,

NSLog(message);
DDLogDebug(message);

我试过了,

#define LOGMESSAGE(message) (NSLog(message);DDLogDebug(message);)

但它给了我编译器错误。

【问题讨论】:

    标签: ios objective-c macros


    【解决方案1】:

    你可以这样写多行宏:

    #define LOGMESSAGE(message) \ NSLog(message); \ DDLogDebug(message); \

    或使用单行版本(无括号):

    #define LOGMESSAGE(message) NSLog(message);DDLogDebug(message);
    

    【讨论】:

    • 非常感谢。它有帮助!
    • 一种技术是使用do { ... } while (0) 包装器,因为它消除了一些棘手错误的可能性。见stackoverflow.com/questions/154136
    猜你喜欢
    • 2011-08-08
    • 1970-01-01
    • 1970-01-01
    • 2010-10-02
    • 2011-01-15
    • 2013-01-15
    • 2022-01-05
    • 2010-10-30
    • 2011-03-31
    相关资源
    最近更新 更多