The magical application of macro definition in app development

Source: Internet
Author: User

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
    • 3. Replace frequently used multiple codes (do not have line breaks)

      **把一个常用的代码包装起来**
#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
    • 4. The macro definition used as a function

      A simple "function" of judging gender

#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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.