There is no macro definition in Swift, and Apple recommends using the let
or get property to override the macro definition value. Although there is no # define, we can still use #if
and match the compiled configuration to complete conditional compilation. Some common macro definitions in Swift project development are listed below, and the source code is provided.
1.Common font Macro Definitions
Import Foundationimport UIKit///System Normal Fontvar gof_systemfontwithsize: (cgfloat), Uifont = {sizeinch returnuifont.systemfontofsize (size);}///System Bold Fontvar gof_boldfontwithsize: (cgfloat), Uifont = {sizeinch returnuifont.boldsystemfontofsize (size);}///used only on the title bar, big title font sizeLet Knavfont = Gof_systemfontwithsize ( -);///Title Font SizeLet Ktitlefont = Gof_systemfontwithsize ( -);///Text SizeLet Kbodyfont = Gof_systemfontwithsize ( -);///Auxiliary Font SizeLet Kassistfont = Gof_systemfontwithsize ( -);
2.Common color Macro Definitions
Import Foundationimport UIKit///generate colors according to RGBA (format: 22,22,22,0.5)var Gof_rgbacolor: (CGFloat, CGFloat, CGFloat, cgfloat), Uicolor = {red, green, blue, alphainch returnUicolor (red:red/255, Green:green/255, Blue:blue/255, Alpha:alpha);}///color generation based on RGB (format: 22,22,22)var Gof_rgbcolor: (CGFloat, CGFloat, cgfloat), Uicolor = {red, green, blueinch returnUicolor (red:red/255, Green:green/255, Blue:blue/255, Alpha:1);}///generate colors based on color values (no transparency) (format 0xffffff)var Gof_colorwithhex: (nsinteger), Uicolor = {hexinch returnUicolor (Red: ((cgfloat) (Hex &0xFF0000) >> -)) /255.0, Green: ((cgfloat) ((Hex &0xff00) >>8)) /255.0, Blue: ((cgfloat) (Hex &0xFF)) /255.0, Alpha:1);}///BlackLet Kbcolor = Gof_colorwithhex (0x000000);///WhiteLet Kwcolor = Gof_colorwithhex (0xFFFFFF)///ColorlessLet Kccolor =Uicolor.clearcolor (); let Kg1color= Gof_colorwithhex (0x323232); let Kg2color= Gof_colorwithhex (0x646464); let Kg3color= Gof_colorwithhex (0x969696); let Kg4color= Gof_colorwithhex (0xc8c8c8);//split lines using only the title barLet Kg5color = Gof_colorwithhex (0XDCDCDC);//Main Page Split lineLet Kg6color = Gof_colorwithhex (0xf0f0f0);//for background gray onlyLet Kbgcolor = Gof_colorwithhex (0xf8f8f8);//Interface Background ColorLet Kholdertipcolor = Gof_colorwithhex (0XAFAFAF);//Hint: Enter the box, the color of the cue languageLet Kbuttonbluecolor = Gof_colorwithhex (0X41ACFF); let Klightcolor= Gof_colorwithhex (0x666666); let Kgraytitlecolor= Gof_colorwithhex (0x999999); let Kgraytipcolor= Gof_colorwithhex (0x757575); let K323232color= Gof_colorwithhex (0x323232); let K646464color= Gof_colorwithhex (0x646464); let K969696color= Gof_colorwithhex (0x969696); let Kf0f0f0color= Gof_colorwithhex (0xf0f0f0);///three tonesLet Kessentialcolor = Gof_colorwithhex (0x46a0f0);//main tone, BlueLet Kassistorangecolor = Gof_colorwithhex (0xff8c28);//complementary tones, orangeLet Kassistgreencolor = Gof_colorwithhex (0x5abe00);//Tonal, Green
3.common variable / method definitions
Import Foundationimport UIKit//MARK:-Print log/** Print Log-parameter message: content of log Messages*/func Printlog<T>(message:t) {#ifDEBUGPrint ("\ (Message)"); #endif}//MARK:-Thread Queue///Main thread QueueLet Kmainthread =dispatch_get_main_queue ();///Global QueueLet Kglobalthread = Dispatch_get_global_queue (Dispatch_queue_priority_default,0);//MARK:-System version///get the system version numberLet ksystemversion =Float (Uidevice.currentdevice (). systemversion);///whether the IOS7 systemLet Kisios7orlater = Int (Uidevice.currentdevice (). systemversion) >=7?true:false;///whether the IOS8 systemLet Kisios8orlater = Int (Uidevice.currentdevice (). systemversion) >=8?true:false;///whether the IOS9 systemLet Kisios9orlater = Int (Uidevice.currentdevice (). systemversion) >=9?true:false;//MARK:-Common width and height///screen BoundsLet Kscreenbounds =Uiscreen.mainscreen (). Bounds;///Screen HeightLet Kscreenheight =Uiscreen.mainscreen (). bounds.size.height;///Screen WidthLet Kscreenwidth =Uiscreen.mainscreen (). Bounds.size.width;///Navigation Bar HeightLet Knavbarheight =44.0;///Status Bar HeightLet Kstatusbarheight =20.0;///tab Bar HeightLet Ktabbarheight =49.0;//get pictures based on picture nameLet Gof_imagewithname: (String)-UIImage? = {ImageNameinch returnUIImage (named:imagename);}
"Note": The above Printlog with conditional compilation, Debug needs to be configured in target, specifically as:
26. How do I define macros in Swift?