IOS开发中常用的一些宏定义

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是不靠谱的,具体原因请看:http://www.jianshu.com/p/528897755dc8)

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 LRRandomColor [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0]

10.设置view的圆角边框

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

11.获取图片资源

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

12.获取当前语言

1
   #define LRCurrentLanguage ([[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
//  CES
 
#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)实现
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS开发,有许多常用的库和框架可以帮助开发者提高效率和加速开发过程。以下是一些常用的库和框架: 1. Alamofire:一个简洁的网络请求库,提供了一种优雅的方式来进行网络请求和处理响应。 2. SDWebImage:一个用于异步加载和缓存网络图片的库,可以帮助提高图片加载性能,并且具有内存和磁盘缓存机制。 3. AlamofireImage:一个基于Alamofire的图片加载库,提供了一些便捷的方法来加载网络图片并进行缓存。 4. SwiftyJSON:一个轻量级的、灵活的JSON解析库,可以帮助简化处理JSON数据的过程。 5. SnapKit:一个优雅的、轻量级的Auto Layout框架,使用Swift语言提供了一种简化UI布局代码的方式。 6. Realm:一个移动数据库框架,提供了高效的数据存储和查询功能,并且支持对象关系映射(ORM)。 7. AlamofireObjectMapper:一个将Alamofire与ObjectMapper结合使用的库,可以方便地将JSON数据映射到模型对象。 8. Kingfisher:一个用于异步加载和缓存网络图片的库,具有高性能和功能丰富的特点。 9. RxSwift:一个用于响应式编程的库,可以简化异步编程和事件处理的复杂性。 10. IQKeyboardManager:一个用于处理键盘弹出和收起的库,可以自动管理键盘,提供了一种简单的方式来避免键盘遮挡输入框的问题。 这只是一小部分常用的库和框架,iOS开发还有许多其他优秀的工具可供选择,根据具体需求选择合适的库和框架进行开发

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值