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

  1. 定义尺寸类的宏

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
DimensMacros.h
//状态栏高度
#define STATUS_BAR_HEIGHT 20
//NavBar高度
#define NAVIGATION_BAR_HEIGHT 44
//状态栏 + 导航栏 高度
#define STATUS_AND_NAVIGATION_HEIGHT ((STATUS_BAR_HEIGHT) + (NAVIGATION_BAR_HEIGHT))
 
//屏幕 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)
 
//屏幕分辨率
#define SCREEN_RESOLUTION (SCREEN_WIDTH * SCREEN_HEIGHT * ([UIScreen mainScreen].scale))
 
 
//广告栏高度
#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.定义沙盒目录文件的宏

1
2
3
4
5
6
7
8
9
10
11
12
13
14
PathMacros.h
//文件目录
#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.工具类的宏

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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)
 
 
// 获取RGB颜色
#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]]))
 
 
//角度转弧度
#define DEGREES_TO_RADIANS(d) (d * M_PI / 180)
 
//大于等于7.0的ios版本
#define iOS7_OR_LATER SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")
 
//大于等于8.0的ios版本
#define iOS8_OR_LATER SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")
 
//iOS6时,导航VC中view的起始高度
#define YH_HEIGHT (iOS7_OR_LATER ? 64:0)
 
//获取系统时间戳
#define getCurentTime [NSString stringWithFormat:@"%ld", (long)[[NSDate date] timeIntervalSince1970]]

4.通知Notification相关的宏

1
2
3
4
5
6
7
8
9
10
11
12
13
NotificationMacros.h
//系统Notification定义
 
#define TNCancelFavoriteProductNotification     @"TNCancelFavoriteProductNotification"      //取消收藏时
#define TNMarkFavoriteProductNotification       @"TNMarkFavoriteProductNotification"        //标记收藏时
 
#define kNotficationDownloadProgressChanged     @"kNotficationDownloadProgressChanged"      //下载进度变化
#define kNotificationPauseDownload              @"kNotificationPauseDownload"               //暂停下载
#define kNotificationStartDownload              @"kNotificationStartDownload"               //开始下载
 
#define kNotificationDownloadSuccess            @"kNotificationDownloadSuccess"             //下载成功
#define kNotificationDownloadFailed             @"kNotificationDownloadFailed"              //下载失败
#define kNotificationDownloadNewMagazine        @"kNotificationDownloadNewMagazine"

服务端API接口的宏

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
APIStringMacros.h
//////////////////////////////////////////////////////////////////////////////////////////////////
//接口名称相关
 
#ifdef DEBUG
//Debug状态下的测试API
#define API_BASE_URL_STRING     @"http://boys.test.companydomain.com/api/"
 
#else
//Release状态下的线上API
#define API_BASE_URL_STRING     @"http://www.companydomain.com/api/"
 
#endif
 
//接口
#define GET_CONTENT_DETAIL      @"channel/getContentDetail" //获取内容详情(含上一个和下一个)
 
#define GET_COMMENT_LIST        @"comment/getCommentList"   //获取评论列表
 
#define COMMENT_LOGIN           @"comment/login"            //获取评论列表
 
#define COMMENT_PUBLISH         @"comment/publish"          //发布评论
 
#define COMMENT_DELETE          @"comment/delComment"       //删除评论
 
#define LOGINOUT                @"common/logout"            //登出

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

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

1
2
3
4
5
6
7
8
9
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"

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

1
2
3
4
5
6
XcodeProjectName-Prefix.pch
#ifdef __OBJC__
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
    #import "Macros.h"
#endif

转 iOS宏定义的使用与规范的更多相关文章

  1. iOS宏定义的使用与规范

    宏定义在很多方面都会使用,例如定义高度.判断iOS系统.工具类,还有诸如文件路径.服务端api接口文档.为了对宏能够快速定位和了解其功能,我们最好在定义的时候将其放入特定的头文件中 定义尺寸类的宏 D ...

  2. ios宏定义字符串

    ios宏定义字符串 #define objcString(str) @""#str"" 使用效果: objcString(字符串)

  3. ios 宏定义 系统版本 判定

    当需要判断iOS系统版本的时候,相信很多人都会这么干: #define SystemVersion [[UIDevice currentDevice] systemVersion].floatValu ...

  4. iOS宏定义

    1.__OBJC__宏定义作用 在.pch 文件中一般都会自动加上这句宏定义,表示宏内引用的文件确保只被使用Objective-C语言的文件所引用,保证引用关系的清晰.因为在一个OC工程中,可能包含. ...

  5. 高效的iOS宏定义

    iOS开发过程中使用一些常用的宏可以提高开发效率,提高代码的重用性:将这些宏放到一个头文件里然后再放到工程中的-Prefix.pch文件中(或者直接放到-Prefix.pch中)直接可以使用,灰常方便 ...

  6. ios宏定义学习

    宏简介: 宏是一种批量处理的称谓.一般说来,宏是一种规则或模式,或称语法替换 ,用于说明某一特定输入(通常是字符串)如何根据预定义的规则转换成对应的输出(通常也是字符串).这种替换在预编译时进行,称作 ...

  7. ios宏定义应该呆在恰当的地方

    项目为了看起来整洁 并减少不必要的多次拼写 我们会把这样的方法 做成宏定义 那么问题来了 很多文件同时用到一个或多个宏定义 写完之后就会变成这个样子 看起来很乱 阅读性也不好 那么问题来了怎么解决嘞 ...

  8. IOS 宏定义一个单例

    有时候是不是因为频繁地创建一个单例对象而头疼,一种方式要写好多遍?当然你可以用OC语言进行封装.但下面将介绍一种由C语言进行的封装.只要实现下面的方法,以后建单例对象只要二句话. 1.新建一个.h文件 ...

  9. iOS 宏定义_16进制色值转化为RGB返回UIColor类型对象

    // 十六进制颜色 #define COLOR_WITH_HEX(hexValue) [UIColor colorWithRed:((float)((hexValue & 0xFF0000) ...

随机推荐

  1. POJ 3169.Layout 最短路

    Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11612   Accepted: 5550 Descripti ...

  2. ubuntu增加工作分区命令

    参考自: https://www.cnblogs.com/wolflowhereu/p/5422653.html ubuntu增加工作分区(workspace)命令 dconf write  /org ...

  3. Linux设置桌面图标 (双击运行jar包)

    Ubuntu平台 预备条件: 1)平台是Gridion上的Ubuntu 2)安装了JRE (版本如下) 3)在IDE(我用的是IDEA)打包成可运行的jar文件 设置步骤: 1)新建.desktop文 ...

  4. @PostConstruct和@PostConstruct 注解 及ehcache 缓存 执行过程 小记

    @PostConstruct 和@PostConstruct 注解 从Java EE 5规范开始,Servlet中增加了两个影响Servlet生命周期的注解(Annotion):@PostConstr ...

  5. vc项目中加载多个lib遇到的问题

    一个VC项目中 在网络加密 json解析等方面  加载了多个第三方库和文件 boost cryptpp rapidjson  mysql的连接池等等 在使用mysql++的时候 多次报错 LNK 20 ...

  6. 深入理解python里面类的对象的赋值

    class T(): def __init__(self): self.name= pass a=T() a.name= b=a #深入理解类,类里面的对象的赋值是指针赋值,也就是同时变的 b.nam ...

  7. SpringBoot定制修改Servlet容器

    1.如何修改Servlet容器的相关配置: 第一种:在application.properties中修改和server有关的配置(ServerProperties提供): server.port=80 ...

  8. Java第15章笔记

    字符串的概述 1.什么是字符串:零个或多个字符组成的有限序列 2.如何使用字符串:(使用字符串分为两步)          1)定义并初始化字符串          2)使用字符,对字符串进行一些处理 ...

  9. c++四舍五入的新方法

    将原来的数加上0.5,如果是需要进位的加上0.5就进位了,如果不需要进位的加上0.5也小于1,被int型省略掉.

  10. windows中当你的键盘无法使用时我们可以用另一种方法哦

    1.使用Win+R打开cmd窗口 2.输入osk回车就出现了一个虚拟的小键盘啦,当你的键盘坏掉后非常实用哦