The magical application of macro definition in development
- 作为常量:替换URL,数值等- 作为常用代码- 等价替换代码,可以当函数使用
Usage rules
- 1.常量以小写k开头- 2.使用下划线连接多个单词(全部大写)
Example
- 1. Replace URL (character constant)
#define kURL_SAVE_CHAT_HISTORY @"http://115.29.40.117:8787/test/app/user/chat/save"
2. Replace common code
One custom RGB color
#define kLIGHT_YELLOW [UIColor colorWithRed:253/255.0 green:231/255.0 blue:211/255.0 alpha:1]//导航条的颜色
Get screen height
#define kSCREEN_HEIGHT [[UIScreen mainScreen] bounds].size.height
#define kALERT(str) [[[UIAlertView alloc]initWithTitle:@"抱歉"message:delegate:nilcancelButtonTitle:@"我知道了"otherButtonTitles:nilnil] show];
How do I use the above code?
kALERT(@"要显示的数据!");
Or the more ultimate ... Configure a large piece of code
#define KCONFIG_MJREFRESH [ Self. TableView Addheaderwithtarget: SelfAction: @selector (Pulldownrefresh)]; Self. Tableview.headerpulltorefreshtext = @"Drop-down refresh"; Self. Tableview.headerreleasetorefreshtext = @"Release immediate Refresh"; Self. Tableview.headerrefreshingtext = @"refreshing, please later ..."; [ Self. TableView Addfooterwithtarget: SelfAction: @selector (Pulluploadmore)]; Self. Tableview.footerpulltorefreshtext = @"Pull up load more data"; Self. Tableview.footerreleasetorefreshtext = @"Release immediate load"; Self. Tableview.footerrefreshingtext = @"Loading, please later ...";//Configuration drop-down refresh
#definekSEXSTRING(sex)[sex intValue]?@"女":@"男"
How to use
kSEXSTRING(@"1");
Summarize
Benefits of using macro definitions
- 1.把所有的常量收集在一起,便于维护- 2.把常用的代码封装之后减少代码量- 3.代码可读性更好,更专业
Click here for more original blog
The magical application of macro definition in app development