1.处理NSLog事件(开发者模式打印,发布者模式不打印)

1
2
3
4
5
  #ifdef DEBUG
  #define NSLog(FORMAT, ...) fprintf(stderr,"%s:%d\t%s\n",[[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String], __LINE__, [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
  #else
  #define NSLog(FORMAT, ...) nil
  #endif

2.在OC语言的情况下导入某些头文件

1
2
3
 #ifdef __OBJC__
       //导入头文件
 #endif

3.处理循环引用问题(处理当前类对象)

1
  #define WS(weakSelf)  __weak __typeof(&*self)weakSelf = self;

4.获取屏幕宽高

1
2
 #define ScreenWidth [[UIScreen mainScreen] bounds].size.width
 #define ScreenHeight [[UIScreen mainScreen] bounds].size.heigh

5.判断iOS8或更高系统版本(谨慎使用,floatValue是不靠谱的)

1
 #define IOS8UP ([[UIDevice currentDevice].systemVersion floatValue] >= 8)

6.设置颜色RGB值

1
 #define RGB(a,b,c) [UIColor colorWithRed:(a/255.0) green:(b/255.0) blue:(c/255.0) alpha:1.0]

7.设置颜色RGB值+透明度

1
 #define RGBA(a,b,c,d) [UIColor colorWithRed:(a/255.0) green:(b/255.0) blue:(c/255.0) alpha:d]

8.支持横屏

1
2
3
4
5
6
7
8
9
 #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000 // 当前Xcode支持iOS8及以上
 #define SCREEN_WIDTH ([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)]?[UIScreen mainScreen].nativeBounds.size.width/[UIScreen mainScreen].nativeScale:[UIScreen mainScreen].bounds.size.width)
 #define SCREENH_HEIGHT ([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)]?[UIScreen mainScreen].nativeBounds.size.height/[UIScreen mainScreen].nativeScale:[UIScreen mainScreen].bounds.size.height)
 #define SCREEN_SIZE ([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)]?CGSizeMake([UIScreen mainScreen].nativeBounds.size.width/[UIScreen mainScreen].nativeScale,[UIScreen mainScreen].nativeBounds.size.height/[UIScreen mainScreen].nativeScale):[UIScreen mainScreen].bounds.size)
 #else
 #define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
 #define SCREENH_HEIGHT [UIScreen mainScreen].bounds.size.height
 #define SCREEN_SIZE [UIScreen mainScreen].bounds.size
 #endif

9.设置随机颜色

1
 #define WRPRandomColor 

UIColor.init(red: CGFloat(arc4random() % 256)/255.0, green: CGFloat(arc4random() % 256)/255.0, blue: CGFloat(arc4random() % 256)/255.0, alpha: 1)

10.设置view的圆角边框

1
2
3
4
5
#define WRPViewBorderRadius(View, Radius, Width, Color)\\
[View.layer setCornerRadius:(Radius)];\
[View.layer setMasksToBounds:YES];\
[View.layer setBorderWidth:(Width)];\
[View.layer setBorderColor:[Color CGColor]]

11.获取图片资源

1
 #define kImage(imageName) [UIImage imageNamed:[NSString stringWithFormat:@"%@",imageName]]

12.获取当前语言

1
  #define WRPCurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0])

13.判断当前的iPhone设备/系统版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//判断是否为iPhone
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
//判断是否为iPad
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
//判断是否为ipod
#define IS_IPOD ([[[UIDevice currentDevice] model] isEqualToString:@"iPod touch"])
// 判断是否为 iPhone 5SE
#define iPhone5SE [[UIScreen mainScreen] bounds].size.width == 320.0f && [[UIScreen mainScreen] bounds].size.height == 568.0f
// 判断是否为iPhone 6/6s
#define iPhone6_6s [[UIScreen mainScreen] bounds].size.width == 375.0f && [[UIScreen mainScreen] bounds].size.height == 667.0f
// 判断是否为iPhone 6Plus/6sPlus
#define iPhone6Plus_6sPlus [[UIScreen mainScreen] bounds].size.width == 414.0f && [[UIScreen mainScreen] bounds].size.height == 736.0f
//获取系统版本
#define IOS_SYSTEM_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]

14.判断是真机还是模拟器

1
#if TARGET_OS_IPHONE//iPhone Device#endif#if TARGET_IPHONE_SIMULATOR//iPhone Simulator#endif

15.沙盒目录文件

1
2
3
4
5
6
//获取temp
#define kPathTemp NSTemporaryDirectory()
//获取沙盒 Document
#define kPathDocument [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
//获取沙盒 Cache
#define kPathCache [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]

16.宏与const 的使用

很多小伙伴在定义一个常量字符串,都会定义成一个宏,最典型的例子就是服务器的地址。在此所有用宏定义常量字符的小伙伴以后就用const来定义吧!为什么呢 ?我们看看:

宏的用法:一般字符串抽成宏,代码抽成宏使用。
const用法:一般常用的字符串定义成const(对于常量字符串苹果推荐我们使用const)。
宏与const区别:

1.编译时刻不同,宏属于预编译 ,const属于编译时刻

2.宏能定义代码,const不能,多个宏对于编译会相对时间较长,影响开发效率,调试过慢,const只会编译一次,缩短编译时间。

3.宏不会检查错误,const会检查错误

通过以上对比,我们以后在开发中如果定义一个常量字符串就用const,定义代码就用宏。

1
static NSString * const loginAccount = @"loginAccount";static NSString * const loginPassword = @"loginPassword";

17.单例化一个类

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
//
//  SynthesizeSingleton.h
//  WRP
 
#ifndef SynthesizeSingleton_h
#define SynthesizeSingleton_h
 
 //声明
#define DECLARE_SYNTHESIZE_SINGLETON_FOR_CLASS(classname) 
 
+ (classname *)sharedInstance; 
 
//实现
#define IMPLEMENT_SYNTHESIZE_SINGLETON_FOR_CLASS(classname) 
 
 static classname *shared##classname = nil; 
 
+ (classname *)sharedInstance 
 
    @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; 
}

使用方法:在你需要创建单例类的类的.h和.m文件中分别加入以下代码(首先导入以上代码所处的头文件)

1
 DECLARE_SYNTHESIZE_SINGLETON_FOR_CLASS(LoginManager)(.h)声明 IMPLEMENT_SYNTHESIZE_SINGLETON_FOR_CLASS(LoginManager)(.m)实现

iOS - 常用的宏定义的更多相关文章

  1. iOS常用的宏定义总结

    字符串是否为空 1   #define kStringIsEmpty(str) ([str isKindOfClass:[NSNull class]] || str == nil || [str le ...

  2. iOS常用define宏定义

    1. 屏幕宽高及常用尺寸 #define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)#define SCREEN_HEIGHT ([U ...

  3. IOS 常用的宏定义(#define)

    开发中经常用到的常量定义(随时更行): 与UIView相关 //获取View的frame属性 #define GetViewWidth(view) view.frame.size.width #def ...

  4. iOS开发笔记--宏定义的黑魔法 - 宏菜鸟起飞手册

    宏定义在C系开发中可以说占有举足轻重的作用.底层框架自不必说,为了编译优化和方便,以及跨平台能力,宏被大量使用,可以说底层开发离开define将寸步难行.而在更高层级进行开发时,我们会将更多的重心放在 ...

  5. iOS开发之--宏定义与const的区别及使用方法

    宏定义的常见用法: 定义一段代码,或指定字符串抽成宏. const(常量): 当有字符串常量的时候,苹果推荐我们使用const,苹果经常把常用的字符串定义成const 宏定义与const的区别: 编译 ...

  6. 在oc中一些常用的宏定义总结

    1.打印CGRect,Size,Point #define NSLogRect(rect) NSLog(@"%s x:%.4f, y:%.4f, w:%.4f, h:%.4f", ...

  7. iOS技巧,宏定义

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAnoAAAPCCAYAAADvRHWgAAAAAXNSR0IArs4c6QAAAZ1pVFh0WE1MOm

  8. iOS: 常用的宏

    Github地址:https://github.com/XFZLDXF/Macro/blob/master/MacroDefinition.h // // MacroDefinition.h // M ...

  9. iOS中常用的宏定义

    转自http://www.jianshu.com/p/be00c3f3cafd //字符串是否为空 #define kStringIsEmpty(str) ([str isKindOfClass:[N ...

随机推荐

  1. Codeforces Round #292 (Div. 2) C. Drazil and Factorial

    题目链接:http://codeforces.com/contest/515/problem/C 给出一个公式例如:F(135) = 1! * 3! * 5!; 现在给你一个有n位的数字a,让你求这样 ...

  2. BZOJ4525——[Usaco2016 Jan]Angry Cows

    1.题意:给一堆可以的限制长度的区间...区间的长度是你控制的...但是只有一个长度...求最短长度覆盖所有的点 2.分析:发现可以二分...那二分吧.....然后我们从头向后扫一遍直接判断能否直接覆 ...

  3. 开源多线程性能测试工具-sysbench

    导读 sysbench是一款开源的多线程性能测试工具,可以执行CPU/内存/线程/IO/数据库等方面的性能测试.数据库目前支持MySQL/Oracle/PostgreSQL.本文主要演示Mysql测试 ...

  4. [BZOJ1999][codevs1167][Noip2007]Core树网的核

    [BZOJ1999][codevs1167][Noip2007]Core树网的核 试题描述 设T=(V, E, W) 是一个无圈且连通的无向图(也称为无根树),每条边带有正整数的权,我们称T为树网(t ...

  5. 忧桑三角形,调了半天,真忧桑TAT

    忧桑三角形 试题描述 小J是一名文化课选手,他十分喜欢做题,尤其是裸题.有一棵树,树上每个点都有点权,现在有以下两个操作: 1. 修改某个点的点权 2. 查询点u和点v构成的简单路径上是否能选出三个点 ...

  6. caffe学习系列(5):激活层介绍

    参考:http://www.cnblogs.com/denny402/p/5072507.html 主要介绍了各个激活函数.

  7. BZOJ 2342: [Shoi2011]双倍回文

    Sol Manacher. 非常裸的Manacher啊...为什么有那么多人写Manacher+并查集?Set?Treap?...好神奇... 你只需要在 \(p[i]++\) 的位置加上判断就可以了 ...

  8. js 函数前的+号

    不知啥时候起,函数的闭包需要增加+才能立即执行了. 不加反而报语法错.orz +function() { console.log("Foo!"); }(); 输出: Foo!< ...

  9. 认识Visual Studio 条件编译

    一开始是在一些源代码中看到这些语法符号,了解到这就是" 条件编译"技术

  10. 【leetcode】Next Permutation

    Next Permutation Implement next permutation, which rearranges numbers into the lexicographically nex ...