Macro definitions are used in many ways, such as defining heights, judging iOS systems, tool classes, and documentation such as file paths and server-side API interfaces. In order to the macro can quickly locate and understand its function, we better in the definition of the time to put it into a specific header file, the following I would like to introduce some commonly used macros to classify, sub-file definition, I hope to be helpful.
Define a macro for a dimension class
dimensmacros.h//status Bar Height # define status_bar_height 20//navbar Height # define navigation_bar_height 44//status bar + navigation bar Height # # Status_and_navigation_height ((status_bar_height) + (navigation_bar_height))//Screen Rect#define SCREEN_RECT ([UIScreen Mainscreen].bounds) #define SCREEN_WIDTH ([UIScreen mainscreen].bounds.size.width) #define Screen_height ([UIScreen Mainscreen].bounds.size.height) #define Content_height (screen_height-navigation_bar_height-status_bar_height)// Screen resolution # define SCREEN_RESOLUTION (Screen_width * screen_height * ([UIScreen Mainscreen].scale))//Banner Height # define Banner_ HEIGHT 215#define stylepage_height 21#define smalltv_height 77#define smalltv_width 110#define FOLLOW_HEIGHT 220#define Subchannel_height 62
2. Define a macro for the sandbox catalog file
pathmacros.h//file directory #define kpathtemp nstemporarydirectory () #define kPathDocument [ Nssearchpathfordirectoriesindomains (Nsdocumentdirectory, nsuserdomainmask, yes) objectAtIndex:0 ] #define kPathCache [nssearchpathfordirectoriesindomains (nscachesdirectory, nsuserdomainmask, YES) objectatindex:0] #define kPathSearch [kpathdocument stringbyappendingpathcomponent:@ "Search.plist"]# define kpathmagazine [kpathdocument stringbyappendingpathcomponent:@ "Magazine"] #define kpathdownloadedmgzs [ kpathmagazine stringbyappendingpathcomponent:@ "Downloadedmgz.plist"] #define kPathDownloadURLs [kpathmagazine stringbyappendingpathcomponent : @ "downloadurls.plist"] #define kPathOperation [kpathmagazine stringbyappendingpathcomponent:@ "Operation.plist"] #define kpathsplashscreen [kpathcache stringbyappendingpathcomponent:@ "SplashScreen"] #endif
3. Macros for tool classes
Utilsmacros.h//log utils marco#define alog (fmt, ...) nslog (@ "%s [line %d] " fmt), __pretty_function__, __line__, # #__VA_ args__); #ifdef debug#define dlog (fmt, ...) nslog (@ "%s [line %d] " fmt), __pretty_function__, __line__, # #__VA_ args__); #else #define dlog (...) #endif #ifdef debug#define ulog (...) #define ulog (fmt, ..) { uialertview *alert = [[uialertview alloc] initwithtitle:[nsstring stringwithformat:@ "%s\n [line %d] ", __pretty_function__, __line__] message:[nsstring stringwithformat:fmt, # #__VA_ARGS__] delegate:nil cancelbuttontitle:@ "OK" otherbuttontitles:nil]; [alert show]; } #else #define ulog (...) #endif//system version utils#define system_version_equal_to (v) ([[[uidevice currentdevice] Systemversion] compare:v options:nsnumericsearch] == nsorderedsame) #define system_ Version_greater_than (v) ([[[ [uidevice currentdevice] systemversion] compare:v options:nsnumericsearch] == nsordereddescending) #define  SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO (v) ([[[uidevice currentdevice] systemversion] compare:v options:nsnumericsearch] != nsorderedascending) #define system_version_less_than (v) ([[[Uidevice currentdevice] systemversion] compare:v options:nsnumericsearch] == nsorderedascending) #define system_version_less _than_or_equal_to (v) ([[[[uidevice currentdevice] systemversion] compare:v options:nsnumericsearch] != nsordereddescending)// get RGB color #define rgba (r,g,b,a) [UIColor colorWithRed:r/255.0f GREEN:G/255.0F BLUE:B/255.0F ALPHA:A] #define  RGB (r,g,b) rgba (r,g,b,1.0f) #define isportrait ([uiapplication sharedapplication].statusbarorientation == uiinterfaceorientationportrait | | [UIApplication sharedApplication].statusBarOrientation == Uiinterfaceorientationportraitupsidedown) #define  ISNILORNULL (_ref) ((_ref) == nil) | | ([(_REF) isequal:[nsnull null]))//Angle to radians #define degrees_to_radians (d) (d * m_pi / 180)//iOS version greater than or equal to 7.0 #define ios7_or_later system_version_greater_than_or_equal _to (@ "7.0")//iOS version #define ios8_or_later system_version_greater_than_or_equal_to (@ "8.0")//greater than or equal to 8.0 IOS6, the starting height of the view in the Navigation VC # # YH_HEIGHT (ios7_or_later ? 64:0)//Get system timestamp #define getcurenttime [nsstring stringwithformat:@ "%ld", (long) [[[nsdate date] timeintervalsince1970]]
4. Notification of notification related macros
notificationmacros.h//system Notification Definition #define tncancelfavoriteproductnotification @ "Tncancelfavoriteproductnotification" //when you cancel a collection #define tnmarkfavoriteproductnotification @ "TNMarkFavoriteProductNotification " //Tag Collection when #define knotficationdownloadprogresschanged @ "knotficationdownloadprogresschanged" //Download Progress Change # define knotificationpausedownload @ "Knotificationpausedownload" //Pause Download #define knotificationstartdownload @ "Knotificationstartdownload" //Start Download #define knotificationdownloadsuccess @ "Knotificationdownloadsuccess" //Download Successful #define knotificationdownloadfailed @ "Knotificationdownloadfailed" //Download Failed #define knotificationdownloadnewmagazine @ " Knotificationdownloadnewmagazine "
Server-side API interface macros
apistringmacros.h////////////////////////////////////////////////////////////////////////////////////////////// Interface name-related #ifdef debug//debug state test api#define api_base_url_string @ " http://boys.test.companydomain.com/api/"#else on-line api#define api_base_url_string in//release state @ "http://www.companydomain.com/api/" #endif//interface #define get_content_detail @ "Channel/getcontentdetail" //get content details (including previous and next) #define GET_COMMENT_LIST @ "Comment/getcommentlist" //get a list of comments #define comment_login @ "Comment/login" //get a list of comments #define comment_publish @ "Comment/publish" //Post Comments #define&nBsp comment_delete @ "Comment/delComment" //Delete Comments #define loginout @ "Common/logout" //Log Out
There are many other types of macros that are not listed here
Create an import of all macros related files Macros.h
Macros.h#import "UtilsMacros.h" #import "APIStringMacros.h" #import "DimensMacros.h" #import "NotificationMacros.h" # Import "SharePlatformMacros.h" #import "StringMacros.h" #import "UserBehaviorMacros.h" #import "PathMacros.h"
In the PCH file for the Xcode project, import the Macros.h file
Xcodeprojectname-prefix.pch#ifdef __objc__ #import <UIKit/UIKit.h> #import <Foundation/Foundation.h> #import "Macros.h" #endif
Usage and specification of IOS macro definitions