宏编译器错误:当参数名称出现在其他位置时出现“解析问题”

问题描述 投票:0回答:2

我尝试制作一个简单的宏来在iOS中创建和显示一个简单的“确定”对话框:

#define ALERT_DIALOG(title,message) \
do\
{\
    UIAlertView *alert_Dialog = [[UIAlertView alloc] initWithTitle:(title) message:(message) delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];\
    [alert_Dialog show];\
} while ( 0 )

如果我尝试在我的代码中使用它:

ALERT_DIALOG(@"Warning", @"Message");

我收到错误:

解析问题。预期为']'

并且错误似乎指向@之前的第二个"Message"

但是,如果我只是复制粘贴宏,则不会出现此错误:

NSString *title = @"Warning";
NSString *message = @"Message";
do
{
    UIAlertView *alert_Dialog = [[UIAlertView alloc] initWithTitle:(title) message:(message) delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert_Dialog show];
} while ( 0 );

是否有一些反对在宏中使用Objective-c构造的东西?还是其他?

objective-c macros preprocessor-directive
2个回答
1
投票

您的宏的问题是,message中的[[[[bothings


0
投票
不是将其声明为宏,而是可以将其声明为C函数:
© www.soinside.com 2019 - 2024. All rights reserved.