Sometimes, we need to make the code simple so that it's easy to read code. We can extract some unchanging stuff and change things as parameters. Defined as a macro, so it's much easier to write.
The following examples illustrate some common macro definitions and share them with you:
1. Determine if the device's operating system is IOS7
1. #define IOS7 ([[[Uidevice currentdevice].systemversion Doublevalue] >= 7.0])
2. Determine if the current device is IPhone5
1. #define KSCREENIPHONE5 ([[[UIScreen Mainscreen] bounds].size.height) >=568)
3. Get the height of the current screen
1. #define Kmainscreenheight ([UIScreen mainscreen].applicationframe.size.height)
4. Get the width of the current screen
1. #define KMAINSCREENWIDTH ([UIScreen mainscreen].applicationframe.size.width)
5. Get RGB color
1. #define Smscolor (R, G, b) [Uicolor colorwithred: (r)/255.0 Green: (g)/255.0 Blue: (b)/255.0 alpha:1.0]
6: Custom Log
#ifdef DEBUG
#define SMSLOG (...) NSLog (__va_args__)
#else
#define SMSLOG (...)
#endif
7. Single case
01.//@interface
#define SINGLETON_INTERFACE (ClassName)
03.+ (ClassName *) shared# #className;
04.
05.
06.//@implementation
#define Singleton_implementation (ClassName)
08.static ClassName *_instance;
09.+ (ID) allocwithzone: (struct _nszone *) zone
10.{
11.static dispatch_once_t Oncetoken;
12.dispatch_once (&oncetoken, ^{
13._instance = [Super Allocwithzone:zone];
14.});
15.return _instance;
16.}
17.+ (ClassName *) shared# #className
18.{
19.static dispatch_once_t Oncetoken;
20.dispatch_once (&oncetoken, ^{
21._instance = [[Self alloc] init];
22.});
23.return _instance;
24.}
Used to determine if a login has been made
#define Islongin [Kdocumentpath stringbyappendingstring:@ "/islongin.plist"]
#define CONTROLLERW [Nssearchpathfordirectoriesindomains (Nscachesdirectory, nsuserdomainmask,yes). FirstObject stringbyappendingstring:@ "/controllerw"]
#define Cancelorderpath [Kdocumentpath stringbyappendingstring:@ "/cancelorderpath"]
Used to temporarily save the current location information
#define Localaddressinfo [Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask,yes). Firstobject stringbyappendingstring:@ "/localaddressinfo"]
#define Userinfoo [Nssearchpathfordirectoriesindomains (Nscachesdirectory, nsuserdomainmask,yes). FirstObject stringbyappendingstring:@ "/userinfo.pliist"]
Save History contacts
#define Hislianxiren [Nssearchpathfordirectoriesindomains (Nscachesdirectory, nsuserdomainmask,yes). FirstObject stringbyappendingstring:@ "/HISLIAWEDWEDNDDEWDF"]
Save Order Number
#define Saveorderno [Nssearchpathfordirectoriesindomains (Nscachesdirectory, nsuserdomainmask,yes). FirstObject stringbyappendingstring:@ "/orderno"]
Save order type
#define Saveordernotype [Nssearchpathfordirectoriesindomains (Nscachesdirectory, nsuserdomainmask,yes). FirstObject stringbyappendingstring:@ "/ordernotype"]
Save the address of the login return information
#define Klongininfopath [Nssearchpathfordirectoriesindomains (Nscachesdirectory, nsuserdomainmask,yes). FirstObject stringbyappendingstring:@ "/localinfo.plist"]
Save History Search record to Local
#define Savehistorysearchrecord [Kdocumentpath stringbyappendingstring:@ "/historysearchrecord.plist"]
Temporarily save selected locations to local
#define Savetemporarysearchrecord [Nssearchpathfordirectoriesindomains (Nscachesdirectory, NSUserDomainMask,YES). Firstobject stringbyappendingstring:@ "/temporarysearchrecordlocalinfo.plist"]
Start coordinates
#define Savestartemporarysearchrecord [Nssearchpathfordirectoriesindomains (Nscachesdirectory, NSUserDomainMask, YES). Firstobject stringbyappendingstring:@ "/starpoint.plist"]
#define Colorgray [Uicolor colorwithred:245/255.0 green:245/255.0 blue:245/255.0 alpha:1]
#define Knavigationbartext self.navigationController.navigationBar.titleTextAttributes = @{ Nsforegroundcolorattributename:krgbcolor (255, 212, ten), Nsfontattributename: [Uifont boldsystemfontofsize:18]};
#define Ktintcolor [Uicolor colorwithred:253/255.0 green:208/255.0 blue:11/255.0 alpha:1.0]
/** navigation bar Title Text Size */
#define KNAVITITLEFONTSIZE 20.0
/** navigation bar Title text color */
#define Knavititlecolor [Uicolor colorwithred:250/255.0 green:190/255.0 blue:1/255.0 alpha:1.0]
#define Kviewbackgroundcolor [Uicolor colorwithred:245/255.0 green:245/255.0 blue:245/255.0 alpha:1.0]
Setting colors with RGB
#define KRGBCOLOR (r,g,b) [Uicolor colorwithred:r/255.0 green:g/255.0 blue:b/255.0 alpha:1.0]
#define KWINDOWH [UIScreen mainscreen].bounds.size.height//Application screen height
#define KWINDOWW [UIScreen mainscreen].bounds.size.width//Application screen width
#define Kappdelegate ((appdelegate*) ([UIApplication sharedapplication].delegate)
#define Kstoryboard (storyboardname) [Uistoryboard storyboardwithname:storyboardname Bundle:nil]
Get the scene object in the corresponding storyboard with the storyboard ID
#define KVCFROMSB (Storyboardid, Storyboardname) [[Uistoryboard storyboardwithname:storyboardname Bundle:nil] \
Instantiateviewcontrollerwithidentifier:storyboardid]
After removing iOS7, the split line margin on the left side of the cell default
#define KREMOVECELLSEPARATOR \
-(void) TableView: (UITableView *) TableView Willdisplaycell: (UITableViewCell *) cell Forrowatindexpath: (Nsindexpath *) Indexpath{\
Cell.separatorinset = uiedgeinsetszero;\
Cell.layoutmargins = Uiedgeinsetszero; \
Cell.preservessuperviewlayoutmargins = NO; \
}
Docment folder Directory
#define Kdocumentpath nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES). FirstObject
#define SCREEN_FRAME ([UIScreen mainscreen].applicationframe)
#define SCREEN_WIDTH ([UIScreen mainscreen].bounds.size.width)
#define Screen_height ([UIScreen mainscreen].bounds.size.height)
#define Colorwhite [Uicolor colorwithred:255.0 green:255.0 blue:255.0 alpha:1]
#define Colorgray [Uicolor colorwithred:245/255.0 green:245/255.0 blue:245/255.0 alpha:1]
#define Colororange [Uicolor colorwithred:254/255.0 green:216/255.0 blue:0.0 alpha:1]
#define Colorblue [Uicolor colorwithred:58/255.0 green:225/255.0 blue:1.0 alpha:1]
#define Colorblack [Uicolor colorwithred:17/255.0 green:17/255.0 blue:7/255.0 alpha:1]
#define Colorline [Uicolor colorwithred:220/255.0 green:220/255.0 blue:220/255.0 alpha:1]
#define Colortext [Uicolor colorwithred:200/255.0 green:200/255.0 blue:200/255.0 alpha:1]
#define Colortextselect [Uicolor colorwithred:248/255.0 green:152/255.0 blue:0/255.0 alpha:1]
#define Colorbigtext [Uicolor colorwithred:254/255.0 green:216/255.0 blue:3/255.0 alpha:1]
#define Colorblacktext [Uicolor colorwithred:45/255.0 green:45/255.0 blue:45/255.0 alpha:1]
#define Font (a) [Uifont fontwithname:@ "Helvetica" size:a]
#define Sysfont (a) [Uifont systemfontofsize:a]
#define IsLogin @ "Loginstyle"
#define LOCA [Nssearchpathfordirectoriesindomains (Nscachesdirectory, nsuserdomainmask,yes). Firstobject stringbyappendingstring:@ "/local.plist"]
#define IS_IPAD (ui_user_interface_idiom () = = Uiuserinterfaceidiompad)
#define Is_iphone (ui_user_interface_idiom () = = Uiuserinterfaceidiomphone)
#define Is_retina ([[UIScreen mainscreen] scale] >= 2.0)
#define SCREEN_WIDTHH ([[UIScreen mainscreen] bounds].size.width)
#define SCREEN_HEIGHTT ([[UIScreen mainscreen] bounds].size.height)
#define SCREEN_MAX_LENGTH (MAX (Screen_widthh, Screen_heightt))
#define SCREEN_MIN_LENGTH (MIN (Screen_width, Screen_heightt))
#define Is_iphone_4_or_less (Is_iphone && screen_max_length < 568.0)
#define IS_IPHONE_5 (is_iphone && screen_max_length = = 568.0)
#define IS_IPHONE_6 (is_iphone && screen_max_length = = 667.0)
#define IS_IPHONE_6P (is_iphone && screen_max_length = = 736.0)
#endif
iOS development-Common macro definitions