您的当前位置:首页正文

iOS中常见的一些宏

来源:要发发知识网

12.获取当前语言
#define LRCurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0])
13.判断当前的iPhone设备/系统版本

//判断是否为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.判断是真机还是模拟器
#if TARGET_OS_IPHONE
//iPhone Device
#endif
#if TARGET_IPHONE_SIMULATOR
//iPhone Simulator
#endif
15.沙盒目录文件

//获取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,定义代码就用宏。

static NSString * const loginAccount = @"loginAccount";

static NSString * const loginPassword = @"loginPassword";

17.单例化一个类

//
//  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文件中分别加入以下代码(首先导入以上代码所处的头文件)

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

18.字符串是否为空
#define kStringIsEmpty(str) ([str isKindOfClass:[NSNull class]] || str == nil || [str length] < 1 ? YES : NO )
19.数组是否为空
#define kArrayIsEmpty(array) (array == nil || [array isKindOfClass:[NSNull class]] || array.count == 0)
20.字典是否为空
#define kDictIsEmpty(dic) (dic == nil || [dic isKindOfClass:[NSNull class]] || dic.allKeys == 0)
21.是否是空对象
#define kObjectIsEmpty(_object) (_object == nil
|| [_object isKindOfClass:[NSNull class]]
|| ([_object respondsToSelector:@selector(length)] && [(NSData *)_object length] == 0)
|| ([_object respondsToSelector:@selector(count)] && [(NSArray *)_object count] == 0))

有问题欢迎指正!!本章内容也会持续更新!谢谢!