iOS:宏定义(项目初期配置)(一)

最近需要重构项目,写个关于重构方面

1.宏定义(基本设置)

//读取App的版本号
#define kAPPVersion [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]
//弱引用
#define kWeakSelf(type) __weak typeof(type) weak##type = type;
//判断设备版iOS9及以上
#define ADVANCE_IOS9 [[[UIDevice currentDevice]systemVersion] floatValue] >= 9.0

//灰色从深到浅
#define Color_33Gray [UIColor colorWithHex:0x333333]//51,51,51
#define Color_66Gray [UIColor colorWithHex:0x666666]//102,102,102
#define Color_99Gray [UIColor colorWithHex:0x999999]//153,153,153
#define Color_BEGray [UIColor colorWithHex:0xBEBEBE]//190,190,190
#define Color_MainGray [UIColor colorWithHex:0xEEEEEE]//238,238,238
#define Color_F0Gray [UIColor colorWithHex:0xF1F1F1]//240,240,240
#define Color_F1Gray [UIColor colorWithHex:0xF1F1F1]//241,241,241

//主红色调
#define Color_mainRed [UIColor colorWithHex:0xBF2129]

//判断对象是否为空,为空的话则返回默认值,如果返回的是NSNumber则装换成NSString
#define M_FIX_NULL_OBJECT(_value,_default) [([_value isEqual:[NSNull null]] || _value==nil)?_default:_value forceTranslateToString]

//默认占位图
#define kDefaultSquareImage WImageByName(@"默认")

//判断是否iphoneX
#define KIsiPhoneX ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) : NO)
//状态栏高度
#define STATUSBARHIGHT [[UIApplication sharedApplication] statusBarFrame].size.height
//tabbar高度
#define TABBARHIGHT [[UIApplication sharedApplication] statusBarFrame].size.height>20 ? 83.0 : 49.0

//用str创建Url
#define kImageUrl(str) [NSURL URLWithString:[str  stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]

//用name创建image
#define WImageByName(name)        [UIImage imageNamed:name]

2.宏定义(系统参数)

//********************************************************************************
#ifdef DEBUG
#define WLog(fmt, ...) NSLog((@"%s [MainThread=%i] [Line %d] " fmt), __PRETTY_FUNCTION__, [NSThread isMainThread], __LINE__, ##__VA_ARGS__);
#else
#define WLog(...)
#endif
#define WLogFunction()  	WLog(@"")
//********************************************************************************


#define WSCREENHEIGHT  [UIScreen mainScreen].bounds.size.height //屏幕高度
#define WSCREENWIDTH   [UIScreen mainScreen].bounds.size.width  //屏幕宽度

#define WRGBA_COLOR(R, G, B, A)  \
        [UIColor colorWithRed:((R) / 255.0f) green:((G) / 255.0f) blue:((B) / 255.0f) alpha:A]
#define WRGB_COLOR(R, G, B)  \
        [UIColor colorWithRed:((R) / 255.0f) green:((G) / 255.0f) blue:((B) / 255.0f) alpha:1.0f]
#define COLOR_WITH_HEX(hexValue) [UIColor colorWithRed:((float)((hexValue & 0xFF0000) >> 16)) / 255.0 green:((float)((hexValue & 0xFF00) >> 8)) / 255.0 blue:((float)(hexValue & 0xFF)) / 255.0 alpha:1.0f]


/******************************************************************************
 *	永久存储对象
 *
 *  NSUserDefaults保存的文件在tmp文件夹里
 *
 *	@param	object      需存储的对象
 *	@param	key         对应的key
 */
#define W_PERSISTENT_SET_OBJECT(object, key)                                                                                                 \
({                                                                                                                                             \
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];                                                                          \
[defaults setObject:object forKey:key];                                                                                                    \
[defaults synchronize];                                                                                                                    \
})

/**
 *	取出永久存储的对象
 *
 *	@param	key     所需对象对应的key
 *	@return	key     所对应的对象
 */
#define W_PERSISTENT_GET_OBJECT(key)  [[NSUserDefaults standardUserDefaults] objectForKey:key]

/**
 *  清除 NSUserDefaults 保存的所有数据
 */
#define W_PERSISTENT_REMOVE_ALLDATA   [[NSUserDefaults standardUserDefaults] removePersistentDomainForName:[[NSBundle mainBundle] bundleIdentifier]]

/**
 *  清除 NSUserDefaults 保存的指定数据
 */
#define W_PERSISTENT_REMOVE(_key)                                         \
({                                                                          \
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];       \
[defaults removeObjectForKey:_key];                                     \
[defaults synchronize];                                                 \
})
///******************************************************************************


//通知
#define POST_NOTIFYCATION(__NAME, __OBJ, __INFO) [[NSNotificationCenter defaultCenter] postNotificationName:__NAME object:__OBJ userInfo:__INFO];
#define ADD_NOTIFYCATION(__OBSERVER, __SELECTOR, __NAME, __OBJ) [[NSNotificationCenter defaultCenter] addObserver:__OBSERVER selector:__SELECTOR name:__NAME object:__OBJ];
#define REMOVE_NOTIFYCATION(__OBSERVER, __NAME, __OBJ) [[NSNotificationCenter defaultCenter] removeObserver:__OBSERVER name:__NAME object:__OBJ];

//判断对象是否为空,为空的话则返回默认值
#define W_StringFix(_value,_default) ([_value isKindOfClass:[NSNull class]] || !_value || _value == nil || [_value isEqualToString:@"(null)"] || [_value isEqualToString:@""] || [_value isEqualToString:@""] || [_value length] == 0)?_default:_value

/**
 *  读取Xib文件的类
 *
 *  @since 1.0
 */
#define WViewByNib(nibName) [[[NSBundle mainBundle] loadNibNamed:nibName owner:self options:nil] lastObject]
#define WNib(nibName) [UINib nibWithNibName:nibName bundle:nil]
#define WRegisterCellNib(tableview,nibName) [tableview registerNib:WNib(nibName) forCellReuseIdentifier:nibName]
#define WRegisterHeaderFooterNib(tableview,nibName) [tableview registerNib:WNib(nibName) forHeaderFooterViewReuseIdentifier:nibName]

//设置随机颜色
#define kRandomColor [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0]

以上是基本宏定义

你可能感兴趣的:(iOS,iOS)