转 iOS宏定义的使用与规范
宏定义在很多方面都会使用,例如定义高度、判断iOS系统、工具类,还有诸如文件路径、服务端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
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宏定义的使用与规范的更多相关文章
- iOS宏定义的使用与规范
宏定义在很多方面都会使用,例如定义高度.判断iOS系统.工具类,还有诸如文件路径.服务端api接口文档.为了对宏能够快速定位和了解其功能,我们最好在定义的时候将其放入特定的头文件中 定义尺寸类的宏 D ...
- ios宏定义字符串
ios宏定义字符串 #define objcString(str) @""#str"" 使用效果: objcString(字符串)
- ios 宏定义 系统版本 判定
当需要判断iOS系统版本的时候,相信很多人都会这么干: #define SystemVersion [[UIDevice currentDevice] systemVersion].floatValu ...
- iOS宏定义
1.__OBJC__宏定义作用 在.pch 文件中一般都会自动加上这句宏定义,表示宏内引用的文件确保只被使用Objective-C语言的文件所引用,保证引用关系的清晰.因为在一个OC工程中,可能包含. ...
- 高效的iOS宏定义
iOS开发过程中使用一些常用的宏可以提高开发效率,提高代码的重用性:将这些宏放到一个头文件里然后再放到工程中的-Prefix.pch文件中(或者直接放到-Prefix.pch中)直接可以使用,灰常方便 ...
- ios宏定义学习
宏简介: 宏是一种批量处理的称谓.一般说来,宏是一种规则或模式,或称语法替换 ,用于说明某一特定输入(通常是字符串)如何根据预定义的规则转换成对应的输出(通常也是字符串).这种替换在预编译时进行,称作 ...
- ios宏定义应该呆在恰当的地方
项目为了看起来整洁 并减少不必要的多次拼写 我们会把这样的方法 做成宏定义 那么问题来了 很多文件同时用到一个或多个宏定义 写完之后就会变成这个样子 看起来很乱 阅读性也不好 那么问题来了怎么解决嘞 ...
- IOS 宏定义一个单例
有时候是不是因为频繁地创建一个单例对象而头疼,一种方式要写好多遍?当然你可以用OC语言进行封装.但下面将介绍一种由C语言进行的封装.只要实现下面的方法,以后建单例对象只要二句话. 1.新建一个.h文件 ...
- iOS 宏定义_16进制色值转化为RGB返回UIColor类型对象
// 十六进制颜色 #define COLOR_WITH_HEX(hexValue) [UIColor colorWithRed:((float)((hexValue & 0xFF0000) ...
随机推荐
- 2015-09-27 git学习
创建 初始化 git init Initialized empty Git repository in <file> 关联 git remote add origin <git@se ...
- Python generator 的yield (enumerate)
生成杨辉三角 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 def triangles(max): L = [1,] while len(L) - 1 < ...
- linux学习第四天 (Linux就该这么学)2018年11月16日
今天主要讲了 管道符,重写向与环境变量 输入输出重写向 标准输出重写向 (标准,覆盖,错误) > 将标准输出重写向到一个文件中 >> 追加到文件 2>错误输出重定向 2> ...
- Subarray Product Less Than K LT713
Your are given an array of positive integers nums. Count and print the number of (contiguous) subarr ...
- linux上的工具或软件
1.下载软件 yum install axelaxel http://mirror.cse.iitk.ac.in/archlinux/iso/2015.04.01/archlinux-2015.04. ...
- linux 和 主机通信的另类方法
偶然发现,linux可以从github上直接下载代码.这样就能用windows写好代码,直接给linux来跑了.很方便. 当然是因为我还不会配置网络来让linux和windows通信.弄了一个下午也没 ...
- centos下安装nodejs
1.首先要安装gcc, # yum install libtool automake autoconf gcc-c++ openssl-devel 2.可以进入某个目录,下载NodeJS v0.10. ...
- 技术课堂】如何管理MongoDB数据库?
- linux_域名映射
vi /etc/hosts在最后加上ip及映射的域名 192.168.229.111 node001 192.168.229.112 node002 192.168.229.113 node003
- 利用Delphi-cross-socket 库提升kbmmw 跨平台开发
以前我写过了,通过httpsys 提升windows 下,delphi 多层应用.随着delphi 10.2 对linux 的支持,很多人也想在linux 下 发布kbmmw 服务器,但是官方仅通过i ...