ios - Fix preprocessor macro -


i have below sample code showing alert view macro

#define show_alert(title,msg,del,cancel,other) \ { \ uialertview *_alert = [[uialertview alloc] initwithtitle:title message:msg delegate:del cancelbuttontitle:cancel otherbuttontitles:other,nil]; \ [_alert show]; \ } while(0); 

to call use

 show_alert(@"error!", @"please check!", nil, @"ok", nil) 

i trying handle error message according string pass

show_alert_statuscode(@"404") // give error 

here tried

#define show_alert_statuscode(code) \ { \ \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); 

but give me below error

enter image description here

i recommend don't use macro make complex code.

just use static method.

__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]; } 

and call static method object reference or using self. exa:-

showalertforstatuscode(400); 

or use this

use macro have used in project

#define alertwithmessage(msg) [[[uialertview alloc] initwithtitle:@"titile" message:msg delegate:nil cancelbuttontitle:@"ok" otherbuttontitles:nil, nil] show] 

Comments