常用宏定义

作者: 我就叫Tom怎么了 | 来源:发表于2017-03-21 15:58 被阅读0次

未完待续 持续更新中

1. 字符串判空处理

#define checkNull(__X__) [(__X__)isEqual:[NSNull null]] || (__X__) == nil ? @"" : [NSString stringWithFormat:@"%@", (__X__)]

2.获取屏幕宽高

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
    #define SCREEN_WIDTH ([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)]?[UIScreen mainScreen].nativeBounds.size.width/[UIScreen mainScreen].nativeScale:[UIScreen mainScreen].bounds.size.width)
    #define SCREEN_HEIGHT ([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)]?[UIScreen mainScreen].nativeBounds.size.height/[UIScreen mainScreen].nativeScale:[UIScreen mainScreen].bounds.size.height)
#else
    #define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
    #define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
#endif

3.Log输出样式修改

#if DEBUG
# define MELog(format, ...) NSLog((@"\n##YourPersonalMessage##\nclass  -->   %s\nmethod -->   %s\nline   -->   %d\nlog    -->   "format@"\n\n\n"), [[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String], __FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
# define MELog(format, ...);
#endif

呈现效果


输出效果

4.宏定义单例

// .h
#define singleton_interface(class) \
+ (instancetype)shared##class;

// .m
#define singleton_implementation(class) \
static class *_instance; \
\
+ (id)allocWithZone:(struct _NSZone *)zone \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [super allocWithZone:zone]; \
}); \
return _instance; \
} \
+ (instancetype)shared##class \
{ \
if (_instance == nil) { \
_instance = [[class alloc] init]; \
} \
return _instance; \
}

5.获取RGB颜色

#define RGBA(r,g,b,a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a] 

6.十六进制颜色设置

#define GRAY_DEEP [UIColor colorWithHexString:@"#666666"]

需要对UIColor增加分类 --- 传送门http://www.jianshu.com/p/19d33ec80e7c

7.对设备系统以及设别型号判断

#define is_iOS7  [[[UIDevice currentDevice]systemVersion] floatValue] >= 7
#define is_iOS8  [[[UIDevice currentDevice]systemVersion] floatValue] >= 8
#define is_iOS9 [[[UIDevice currentDevice] systemVersion] floatValue] >= 9

#define is_iPhone4 ([UIScreen mainScreen].bounds.size.height == 480)
#define is_iPhone5 ([UIScreen mainScreen].bounds.size.height == 568)
#define is_iPhone6 ([UIScreen mainScreen].bounds.size.height == 667)
#define is_iPhone6P ([UIScreen mainScreen].bounds.size.height == 1104)

#define is_iPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

8.WeakSelf

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

9.判断是否存在某文件(开发三方库用的上,比如webImage功能,判断一下,如果对方使用的是SDWebImage就使用SD进行相关操作,如果对方用的是YYKit,那么就使用YY相关的功能)

#if __has_include(<YYKit/UIImageView+YYWebImage.h>)
    NSLog(@"include");
#else
    NSLog(@"not include");
#endif

注:

宏定义可以写在当前类,也可以写全局引用文件里.PCH创建及使用方法传送门
http://www.jianshu.com/p/b4ff30f85019
主要是个人记录使用,也为大家提供一个方便.随时更新.

相关文章

  • iOS 常用宏定义

    常用宏定义

  • iOS开发常用的宏定义

    Objective-C常用宏/*! 字体 */ /*! 颜色宏定义 */ /*! 弱引用宏 */ /*! 输出显示...

  • 常用宏定义

    // HGBMacroConfig.h #ifndef HGBMacroConfig_h #define HGBM...

  • 常用宏定义

    //屏幕尺寸 define SCREEN_WIDTH [UIScreen mainScreen].bounds...

  • 常用宏定义

    1.color #define COlOR(R,G,B,A) [UIColor colorWithRed:(R)/...

  • 常用宏定义

    1、屏幕尺寸 2、UIView相关 3、常用颜色 4、手机版本 5、沙盒

  • 常用宏定义

    未完待续 持续更新中 1. 字符串判空处理 2.获取屏幕宽高 3.Log输出样式修改 呈现效果 4.宏定义单例 5...

  • 常用宏定义

    优点 提高了程序的可读性,同时也方便进行修改,用户只需要在一处定义,多处使用,修改也只需要修改一处 提高程序的运行...

  • iOS 常用宏定义

    iOS 开发中使用一些常用宏定义可以大大提高开发效率,提高代码的重用性.以下是一些常用的宏定义: 像这些宏定义,在...

  • 宏定义指令

    常用的宏定义指令 详细应用

网友评论

    本文标题:常用宏定义

    本文链接:https://www.haomeiwen.com/subject/nebwnttx.html