【问题标题】:Fix preprocessor macro修复预处理器宏
【发布时间】:2016-01-29 09:31:32
【问题描述】:

我有以下示例代码,用于将警报视图显示为宏

#define SHOW_ALERT(title,msg,del,cancel,other) \
do { \
UIAlertView *_alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:del cancelButtonTitle:cancel otherButtonTitles:other,nil]; \
[_alert show]; \
} while(0);

调用使用

 SHOW_ALERT(@"Error!", @"Please Check!", nil, @"Ok", nil)

我正在尝试根据我的字符串传递来处理错误消息

SHOW_ALERT_STATUSCODE(@"404") // give error

这是我尝试过的

#define SHOW_ALERT_STATUSCODE(code) \
do { \
\NSString *errorMsg=@"";\
if([Status_code isEqualtoString:@"404"])\
\{errorMsg=@"Page Not Found";}\
\else if([Status_code isEqualtoString:@"401"])\
\{errorMsg=@"Authentication Failed";}\
\
UIAlertView *_alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:errorMsg delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok",nil]; \
[_alert show]; \
} while(0);

但它给了我以下错误

【问题讨论】:

  • UIAlertView 现在已弃用,请参阅 UIAlertController 代替。
  • 帮自己和世界其他人一个忙,创建一个函数或方法。是时候意识到你现在生活在 21 世纪。如果你坚持 do { } while (0) 然后去掉最后的分号。
  • 找出我的答案。 . . .如果需要更多帮助,请发表评论。

标签: ios objective-c iphone macros


【解决方案1】:

在某些行的开头有虚假的反斜杠:

\NSString *errorMsg=@"";\

\{errorMsg=@"Page Not Found";}\

\else if([Status_code isEqualtoString:@"401"])\

你也传入code,但测试Status_code

我建议扔掉那些笨拙的垃圾,而是创建一个易于阅读和维护的方法:

- (void)showAlertForStatusCode:(NSUInteger)code
{
    NSString errorMsg = nil;
    if (code == 404)
        errorMsg = @"Page Not Found";
    else if (code == 401)
        errorMsg = @"Authentication Failed";
    else
        errorMsg = @"Unknown error";
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" 
                                                    message:errorMsg
                                                   delegate:self
                                          cancelButtonTitle:nil 
                                          otherButtonTitles:@"Ok", nil];
    [alert show];
}

【讨论】:

  • 宏定义怎么做?
  • @iphonemaclover 去掉虚假的反斜杠。
【解决方案2】:

我建议不要使用宏,因为它会产生复杂的代码。

只需使用静态方法。

__unused static void showAlertForStatusCode(NSUInteger code)
{
    NSString *errorMsg = nil;
    if (code == 404)
        errorMsg = @"Page Not Found";
    else if (code == 401)
        errorMsg = @"Authentication Failed";
    else
        errorMsg = @"Unknown error";
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert"
                                                    message:errorMsg
                                                   delegate:nil
                                          cancelButtonTitle:nil
                                          otherButtonTitles:@"Ok", nil];
    [alert show];
}

然后您使用对象引用或使用 self. 例:-

showAlertForStatusCode(400);

或使用此

使用我在项目中使用的这个宏

#define AlertWithMessage(msg) [[[UIAlertView alloc] initWithTitle:@"Titile" message:msg delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil] show]

【讨论】:

  • 如果由于 __unused 而出现错误,则将其删除并仅使用 static
  • 警报视图委托未设置。
  • 你可以设置委托。 . .
【解决方案3】:

试试这个。

  #define SHOW_ALERT_STATUSCODE(code)                     \
  \
  do {                                                    \
  \
 NSString *errorMsg=@"";                                  \
 if([code isEqualToString:@"404"])                     \
 {\
    errorMsg=@"Page Not Found";                           \
 }\
 else if([code isEqualToString:@"401"])                 \
 {\
     errorMsg=@"Authentication Failed";                   \
 }\
  \
   UIAlertView *_alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:errorMsg delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok",nil];                                 \
  \
  [_alert show];                                          \
} while(0);                                               \

除了前导的“\”之外,您的代码没有任何问题。

【讨论】:

  • 该代码不起作用。 @"code" 不是你想要的,你还要测试@"401" == @"401"。同样在 OP 的原始代码中,他无论如何都在传递一个字符串。
猜你喜欢
  • 1970-01-01
  • 2015-04-15
  • 2015-07-31
  • 1970-01-01
  • 2010-09-19
  • 2020-02-09
  • 1970-01-01
  • 2011-01-26
相关资源
最近更新 更多