天天看点

iOS宏定义的使用与规范

http://my.oschina.net/leejan97/blog/354904

宏定义在很多方面都会使用,例如定义高度、判断iOS系统、工具类,还有诸如文件路径、服务端api接口文档。为了对宏能够快速定位和了解其功能,我们最好在定义的时候将其放入特定的头文件中,下面我抛砖引玉,对一些常用的宏进行分类、分文件定义,希望对大家有所帮助。

定义尺寸类的宏

<a href="http://my.oschina.net/leejan97/blog/354904">?</a>

<code>DimensMacros.h</code>

<code>//状态栏高度</code>

<code>#define STATUS_BAR_HEIGHT 20</code>

<code>//NavBar高度</code>

<code>#define NAVIGATION_BAR_HEIGHT 44</code>

<code>//状态栏 + 导航栏 高度</code>

<code>#define STATUS_AND_NAVIGATION_HEIGHT ((STATUS_BAR_HEIGHT) + (NAVIGATION_BAR_HEIGHT))</code>

<code>//屏幕 rect</code>

<code>#define SCREEN_RECT ([UIScreen mainScreen].bounds)</code>

<code>#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)</code>

<code>#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)</code>

<code>#define CONTENT_HEIGHT (SCREEN_HEIGHT - NAVIGATION_BAR_HEIGHT - STATUS_BAR_HEIGHT)</code>

<code>//屏幕分辨率</code>

<code>#define SCREEN_RESOLUTION (SCREEN_WIDTH * SCREEN_HEIGHT * ([UIScreen mainScreen].scale))</code>

<code>//广告栏高度</code>

<code>#define BANNER_HEIGHT 215</code>

<code>#define STYLEPAGE_HEIGHT 21</code>

<code>#define SMALLTV_HEIGHT 77</code>

<code>#define SMALLTV_WIDTH 110</code>

<code>#define FOLLOW_HEIGHT 220</code>

<code>#define SUBCHANNEL_HEIGHT 62</code>

2.定义沙盒目录文件的宏

<code>PathMacros.h</code>

<code>//文件目录</code>

<code>#define kPathTemp                   NSTemporaryDirectory()</code>

<code>#define kPathDocument               [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]</code>

<code>#define kPathCache                  [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0]</code>

<code>#define kPathSearch                 [kPathDocument stringByAppendingPathComponent:@"Search.plist"]</code>

<code>#define kPathMagazine               [kPathDocument stringByAppendingPathComponent:@"Magazine"]</code>

<code>#define kPathDownloadedMgzs         [kPathMagazine stringByAppendingPathComponent:@"DownloadedMgz.plist"]</code>

<code>#define kPathDownloadURLs           [kPathMagazine stringByAppendingPathComponent:@"DownloadURLs.plist"]</code>

<code>#define kPathOperation              [kPathMagazine stringByAppendingPathComponent:@"Operation.plist"]</code>

<code>#define kPathSplashScreen           [kPathCache stringByAppendingPathComponent:@"splashScreen"]</code>

<code>#endif</code>

3.工具类的宏

<code>UtilsMacros.h</code>

<code>//Log utils marco</code>

<code>#define ALog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);</code>

<code>#ifdef DEBUG</code>

<code>#define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);</code>

<code>#else</code>

<code>#define DLog(...)</code>

<code>#define ULog(...)</code>

<code>//#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]; }</code>

<code>//System version utils</code>

<code>#define SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)</code>

<code>#define SYSTEM_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)</code>

<code>#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)</code>

<code>#define SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)</code>

<code>#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)</code>

<code>// 获取RGB颜色</code>

<code>#define RGBA(r,g,b,a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]</code>

<code>#define RGB(r,g,b) RGBA(r,g,b,1.0f)</code>

<code>#define IsPortrait ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)</code>

<code>#define IsNilOrNull(_ref)   (((_ref) == nil) || ([(_ref) isEqual:[NSNull null]]))</code>

<code>//角度转弧度</code>

<code>#define DEGREES_TO_RADIANS(d) (d * M_PI / 180)</code>

<code>//大于等于7.0的ios版本</code>

<code>#define iOS7_OR_LATER SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")</code>

<code>//大于等于8.0的ios版本</code>

<code>#define iOS8_OR_LATER SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")</code>

<code>//iOS6时,导航VC中view的起始高度</code>

<code>#define YH_HEIGHT (iOS7_OR_LATER ? 64:0)</code>

<code>//获取系统时间戳</code>

<code>#define getCurentTime [NSString stringWithFormat:@"%ld", (long)[[NSDate date] timeIntervalSince1970]]</code>

4.通知Notification相关的宏

<code>NotificationMacros.h</code>

<code>//系统Notification定义</code>

<code>#define TNCancelFavoriteProductNotification     @"TNCancelFavoriteProductNotification"      //取消收藏时</code>

<code>#define TNMarkFavoriteProductNotification       @"TNMarkFavoriteProductNotification"        //标记收藏时</code>

<code>#define kNotficationDownloadProgressChanged     @"kNotficationDownloadProgressChanged"      //下载进度变化</code>

<code>#define kNotificationPauseDownload              @"kNotificationPauseDownload"               //暂停下载</code>

<code>#define kNotificationStartDownload              @"kNotificationStartDownload"               //开始下载</code>

<code>#define kNotificationDownloadSuccess            @"kNotificationDownloadSuccess"             //下载成功</code>

<code>#define kNotificationDownloadFailed             @"kNotificationDownloadFailed"              //下载失败</code>

<code>#define kNotificationDownloadNewMagazine        @"kNotificationDownloadNewMagazine"</code>

服务端API接口的宏

<code>APIStringMacros.h</code>

<code>//////////////////////////////////////////////////////////////////////////////////////////////////</code>

<code>//接口名称相关</code>

<code>//Debug状态下的测试API</code>

<code>#define API_BASE_URL_STRING     @"http://boys.test.companydomain.com/api/"</code>

<code>//Release状态下的线上API</code>

<code>#define API_BASE_URL_STRING     @"http://www.companydomain.com/api/"</code>

<code>//接口</code>

<code>#define GET_CONTENT_DETAIL      @"channel/getContentDetail" //获取内容详情(含上一个和下一个)</code>

<code>#define GET_COMMENT_LIST        @"comment/getCommentList"   //获取评论列表</code>

<code>#define COMMENT_LOGIN           @"comment/login"            //获取评论列表</code>

<code>#define COMMENT_PUBLISH         @"comment/publish"          //发布评论</code>

<code>#define COMMENT_DELETE          @"comment/delComment"       //删除评论</code>

<code>#define LOGINOUT                @"common/logout"            //登出</code>

还有很多其他类型的宏,此处不一一列举

创建一个import所有宏相关的文件Macros.h

<code>Macros.h</code>

<code>#import "UtilsMacros.h"</code>

<code>#import "APIStringMacros.h"</code>

<code>#import "DimensMacros.h"</code>

<code>#import "NotificationMacros.h"</code>

<code>#import "SharePlatformMacros.h"</code>

<code>#import "StringMacros.h"</code>

<code>#import "UserBehaviorMacros.h"</code>

<code>#import "PathMacros.h"</code>

在xcode项目的pch文件中,导入Macros.h文件

<code>XcodeProjectName-Prefix.pch</code>

<code>#ifdef __OBJC__</code>

<code>    </code><code>#import &lt;UIKit/UIKit.h&gt;</code>

<code>    </code><code>#import &lt;Foundation/Foundation.h&gt;</code>

<code>    </code><code>#import </code><code>"Macros.h"</code>

如何联系我:【万里虎】www.bravetiger.cn

【QQ】3396726884 (咨询问题100元起,帮助解决问题500元起)

【博客】http://www.cnblogs.com/kenshinobiy/

继续阅读