Objective-C 常用宏, 自定义log, 单例, GCD,Socket,RGB

#ifndef PrefixHeader_pch
#define PrefixHeader_pch

#pragma mark - ====================  头文件
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>


#pragma mark - ====================  强弱self
#define kWeakSelf(weakSelf)      __weak   __typeof(&*self)weakSelf   = self;
#define kStrongSelf(strongSelf)  __strong __typeof(&*self)strongSelf = self;

#pragma mark - ====================  log
#ifdef DEBUG  //打印控制器名称 方法名称 所在行数
#   define SSLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
#   define SSLog(...)
#endif

#pragma mark - ====================  frame  font
//frame
#define ONE_PX          1/[[UIScreen mainScreen] scale]
#define SCREEN_WIDTH    ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT   ([UIScreen mainScreen].bounds.size.height)
#define SCREEN_BOUNDS   [UIScreen mainScreen].bounds
//font
#define kFont(x)        [UIFont systemFontOfSize:x]
#define kBoldFont(x)    [UIFont boldSystemFontOfSize:x]
//程序的本地化,引用国际化的文件
#define MyLocal(x, ...) NSLocalizedString(x, nil)


#pragma mark - ====================  getSysMessage
//获取系统版本  SSLog(@"%.1lf",kCurrentSystemVersion);  //保留几位小数
#define kCurrentSystemVersion [[[UIDevice currentDevice] systemVersion] floatValue]
//获取当前语言
#define kCurrentLanguage      ([[NSLocale preferredLanguages] objectAtIndex:0])
#define IOS7   ([[UIDevice currentDevice].systemVersion doubleValue] >= 7.0]
#define IOS8   ([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0)
#define iOS9   ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0)

#define isRetina ([UIScreen instancesRespondToSelector:@selector(currentMode)] ?CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO)
#define iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ?CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
#define isPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

//判断是真机还是模拟器
#if TARGET_OS_IPHONE
//iPhone Device
#endif
#if TARGET_IPHONE_SIMULATOR
//iPhone Simulator
#endif

//使用ARC和不使用ARC
#if __has_feature(objc_arc)
//compiling with ARC
#else
// compiling without ARC
#endif



#pragma mark - ====================  image color
//定义UIImage对象
#define kImageNamed(_pointer)    [UIImage imageNamed:[UIUtil imageName:_pointer]]
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
#define colorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
#define kClearColor [UIColor clearColor]

//带有RGBA的颜色设置
#define colorWithR_G_B(R, G, B, A) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:A]
//生成一个随机颜色
#define random(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)/255.0]
#define randomColor random(arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256))




#pragma mark - ====================  GCD
// GLOBAL_THREAD(^{ code });
#define GLOBAL_THREAD(block) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block)
#define MAIN_THREAD(block)   dispatch_async(dispatch_get_main_queue(),block)


#pragma mark - ====================  通知中心
#define kNotificationCenter    [NSNotificationCenter defaultCenter]


#pragma mark - ====================  文件管理相关
#define kUserDefault [NSUserDefaults standardUserDefaults]  //userDefault
#define kFileManager [NSFileManager defaultManager]         //fileManager
#define kDocmentPath NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject

#pragma mark - ====================  单例一个类
// .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; \
}


//由角度获取弧度 有弧度获取角度
#define degreesToRadian(x) (M_PI * (x) / 180.0)
#define radianToDegrees(radian) (radian*180.0)/(M_PI)



#endif


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值