Using some commonly used macros during iOS development can improve development efficiency and code reusability; placing these macros in a header file and then putting them in the project-prefix. in the PCH file (or directly put it in-prefix. in PCH.
This article is organized from http://www.cocoachina.com/applenews/devnews/2013/0328/5907.html.
You can add, delete, or modify these macros based on your habits.
//// Macrodefinition. h // macrodefinitiondemo /// created by wind on 13-6-9. // copyright (c) 2013. all rights reserved. // # ifndef macrodefinition_h # define macrodefinition_h // specify the device size. // navbar height # define navigationbar_height 44 // obtain the screen width and height # define screen_width ([uiscreen mainscreen]. bounds. size. width) # define screen_height ([uiscreen mainscreen ]. Bounds. size. height) // ------------------- get the device size ------------------------- // ------------------- print the log in debug mode. The current line # ifdef debug # define dlog (FMT ,...) nslog (@ "% s [line % d]" FMT), _ pretty_function __, _ line __, ##__ va_args __); # else # define dlog (...) # endif // rewrite nslog. Print the log and current number of lines in debug mode # If debug # define nslog (format ,...) fprintf (stderr, "\ nfunction: % s line: % d content: % s \ n ",_ _ FUNCTION __, _ line __, [[nsstring stringwithformat: format, ##__va_args _] utf8string]); # else # define nslog (format ,...) nil # endif // print logs in debug mode. A warning is displayed for the current row. # ifdef debug # 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 Cance Lbuttontitle: @ "OK" otherbuttontitles: Nil]; [alert show] ;}# else # define ulog (...) # endif # define ittdebug # define limit 10 # define limit 3 # define limit 1 # ifndef ittmaxloglevel # ifdef debug # define ittmaxloglevel limit # else # define ittmaxloglevel limit # endif // general purpose logger. this ignores logging levels. # ifdef ittdebug # define it Tdprint (XX ,...) nslog (@ "% s (% d):" XX, _ pretty_function __, _ line __, ##__va_args __) # else # define ittdprint (XX ,...) (void) 0) # endif // prints the current method's name. # define ittdprintmethodname () ittdprint (@ "% s", _ pretty_function _) // log-level based logging macros. # If ittloglevel_error <= ittmaxloglevel # define ittderror (XX ,...) ittdprint (XX, ##__ va_args _) # else # define ittderror (XX ,...) (void) 0) # endif # If ittloglevel_warning <= ittmaxloglevel # define ittdwarning (XX ,...) ittdprint (XX, ##__ va_args _) # else # define ittdwarning (XX ,...) (void) 0) # endif # If ittloglevel_info <= ittmaxloglevel # define ittdinfo (XX ,...) ittdprint (XX, ##__ va_args _) # else # define ittdinfo (XX ,...) (void) 0) # endif # ifdef ittdebug # define ittdconditionlog (condition, XX ,...) {If (condition) {\ ittdprint (XX, ##__ va_arg S _) ;\}\} (void) 0) # else # define ittdconditionlog (condition, XX ,...) (void) 0) # endif # define ittassert (condition ,...) \ do {\ If (! (Condition) {\ [nsassertionhandler currenthandler] \ response: [nsstring stringwithuf8string :__ pretty_function _] \ file: [nsstring stringwithuf8string :__ file _] \ linenumber: __line _ \ description :__ va_args _]; \}\} while (0) // --------------------- print logs -------------------------- // ------------------------ system -------------------------- // obtain the system version # define ios_version [[uidevice currentdevice] Systemversion] floatvalue] # define currentsystemversion [[uidevice currentdevice] systemversion] // obtain the current language # define currentlanguage ([[nslocale preferredlanguages] objectatindex: 0]) // determine whether the Retina screen, device % fhone 5, and iPad # define isretina ([uiscreen instancesrespondtoselector: @ selector (currentmode)]? Cgsizeequaltosize (cgsizemake (640,960), [[uiscreen mainscreen] currentmode]. Size): No) # define iphone5 ([uiscreen instancesrespondtoselector: @ selector (currentmode)]? Cgsizeequaltosize (cgsizemake (640,113 6), [[uiscreen mainscreen] currentmode]. size): No) # define ispad (ui_user_interface_idiom () = uiuserinterfaceidiompad) // determine whether it is a real machine or a simulator # If target_ OS _iphone // iPhone device # endif # If target_iphone_simulator // iPhone simulator # endif // check the system version # define system_version_to _to (V) ([[uidevice currentdevice] systemversion] compare: V Options: nsnumericsearch] = nsorderedsame) # d Efine system_version_greater_than (V) ([[uidevice currentdevice] systemversion] compare: V Options: nsnumericsearch] = nsordereddescending) # define Merge (V) ([[[uidevice currentdevice] systemversion] compare: V Options: nsnumericsearch]! = Nsorderedascending) # define system_version_less_than (V) ([[uidevice currentdevice] systemversion] compare: V Options: nsnumericsearch] = nsorderedascending) # define sequence (V) ([[[uidevice currentdevice] systemversion] compare: V Options: nsnumericsearch]! = Nsordereddescending) // ------------------------ system running // ------------------------ memory ---------------------------- // use arc and do not use arc # If _ has_feature (objc_arc) // compiling with arc # else // compiling without arc # endif # pragma mark-common functions # define release_safely (_ pointer) {[_ pointer release]; _ pointer = nil;} // release an object # define safe_delete (p) if (p) {[P release], P = nil;} # define safe_release (X) [x release]; X = nil // ---------------------- memory usage // ---------------------- image reading // read local images # define LoadImage (file, ext) [uiimage imagewithcontentsoffile: [[nsbundle mainbundle] pathforresource: file oftype: ext] // defines the uiimage object # define image (a) [uiimage imagewithcontentsoffile: [[nsbundle mainbundle] pathforresource: A oftype: nil] // define the uiimage object # define imagenamed (_ pointer) [uiimage imagenamed: [uiutil imagename: _ pointer] // we recommend that you use the first two macro definitions, higher performance than the latter // ------------------------ image ------------------------------ // ---------------------- color class expires // RGB color conversion (hexadecimal-> hexadecimal) # define uicolorfromrgb (rgbvalue) [uicolor colorwithred :( (float) (rgbvalue & 0xff0000)> 16)/255.0 green :( (float) (rgbvalue & 0xff00)> 8 )) /255.0 blue :( (float) (rgbvalue & 0xff)/255.0 ALPHA: 1.0] // color settings with rgba # define color (R, G, B,) [uicolor colorwithred: R/255.0 Green: G/255.0 Blue: B/255.0 ALPHA: A] // obtain the RGB color # define rgba (R, G, B,) [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) // background color # define background_color [uicolor colorwithred: 242.0/255.0 Green: 236.0/255.0 Blue: 231.0/255.0 ALPHA: 1.0] // clear the background color # define clearcolor [uicolor clearcolor] # pragma mark-color functions # define rgbcolor (R, G, B) [uicolor colorwithred :( R) /255.0f green :( G)/255.0f blue :( B)/255.0f ALPHA: 1] # define rgbacolor (R, G, B, A) [uicolor colorwithred :( R) /255.0f green :( G)/255.0f blue :( B)/255.0f Alpha :( A)] // ---------------------- color class ---------------------------- // ------------------------ other fonts // simplified font definition of 正 # define font (f) [uifont fontwithname: @ "FZHTJW--GB1-0" Size: f] // define an API # define apiurl @ "http: // XXXXX/" // log on to the API # define apilogin [apiurl stringbyappendingstring: @ "login"] // sets the tag attribute of the view # define viewwithtag (_ object, _ Tag) [_ OBJECT viewwithtag: _ Tag] // localized the program, reference an international file # define mylocal (x ,...) nslocalizedstring (x, nil) // G-C-D # define back (Block) dispatch_async (dispatch_get_global_queue (queue, 0), block) # define main (Block) dispatch_async (blocks) // nsuserdefaults instantiation # define user_default [nsuserdefaults standarduserdefaults] // obtain the radians and radians from the angle to obtain the angle # define degreestoradian (x) (m_pi * (X)/180.0) # define radiantodegrees (radian) (radian * 180.0)/(m_pi) // list a class # define synthesize_singleton_for_class (classname) \ static classname * shared # classname = nil; \ + (classname *) shared # classname \ {\ @ synchronized (Self) \ {\ If (shared ## classname = nil) \ {\ shared # classname = [[self alloc] init] ;\}\}\ return shared # classname ;\}\+ (ID) allocwithzone :( nszone *) Zone \ {\@ synchronized (Self) \ {\ if (shared # classname = nil) \ {\ shared # classname = [Super allocwithzone: zone]; \ return shared # classname ;\}\\ return nil ;\}\- (ID) copywithzone :( nszone *) Zone \{\ return self; \} # endif
Macrodefinition. h: https://github.com/XFZLDXF/Macro
Csdn download: http://download.csdn.net/detail/duxinfeng2010/5555367