源码

首页 » 归档 » 源码 » iOS中常见的一些宏-ios学习从入门到精通尽在姬长信

iOS中常见的一些宏-ios学习从入门到精通尽在姬长信

分享最热门的ios资讯

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

  #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语言的情况下导入某些头文件

 #ifdef __OBJC__
       //导入头文件
 #endif

3.处理循环引用问题(处理当前类工具)

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

4.获取屏幕宽高

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

5.判断iOS8或更高系统版本(谨慎使用,floatValue是不靠谱的,具体原因请看:http://www.jianshu.com/p/528897755dc8)

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

6.设置颜色RGB值

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

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

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

8.支持横屏

 #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.设置随机颜色

 #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的圆角边框

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

11.获取图片资源

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

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)实现

文章转自 上吊的豆腐的简书
用意志战胜身体的惰性!

(0)

本文由 姬長信 创作,文章地址:https://blog.isoyu.com/archives/1157.html
采用知识共享署名4.0 国际许可协议进行许可。除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。最后编辑时间为:9 月 8, 2016 at 07:59 下午

关键词:

热评文章

发表回复

[必填]

我是人?

提交后请等待三秒以免造成未提交成功和重复