IOS基础:常用的宏

2020-11-25  本文已影响0人  时光啊混蛋_97boy

原创:知识点总结性文章
创作不易,请珍惜,之后会持续更新,不断完善
个人比较喜欢做笔记和写总结,毕竟好记性不如烂笔头哈哈,这些文章记录了我的IOS成长历程,希望能与大家一起进步
温馨提示:由于简书不支持目录跳转,大家可通过command + F 输入目录标题后迅速寻找到你所需要的内容

目录

Block

// void
typedef void (^VoidBlock)(void);
typedef void (^ClickBlock)(void);
typedef void (^ClickIndexBlock)(NSInteger index);
typedef BOOL (^BoolBlock)(void);
typedef int  (^IntBlock)(void);
typedef id   (^IDBlock)(void);

// int
typedef void (^Void_IntBlock)(int index);
typedef BOOL (^Bool_IntBlock)(int index);
typedef int  (^Int_IntBlock)(int index);
typedef id   (^ID_IntBlock)(int index);

// string
typedef void (^Void_StringBlock)(NSString *str);
typedef BOOL (^Bool_StringBlock)(NSString *str);
typedef int  (^Int_StringBlock)(NSString *str);
typedef id   (^ID_StringBlock)(NSString *str);

// id
typedef void (^Void_IDBlock)(id data);
typedef BOOL (^Bool_IDBlock)(id data);
typedef int  (^Int_IDBlock)(id data);
typedef id   (^ID_IDBlock)(id data);

// bool
typedef void (^VoidBoolBlock)(BOOL flag);

调用方式

@property (nonatomic, copy) ClickBlock clickBlock;

判断系统版本

#define SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)

调用方式

- (void)testSystemVersion
{
    if (SYSTEM_VERSION_GREATER_THAN(@"13.0"))
    {
        NSLog(@"系统当前版本号为14.2");
    }
}

输出结果

2020-11-25 15:35:11.792044+0800 Macro[78684:5115639] 系统当前版本号为14.2

移除Nil

扩展方法的助攻

//nil  转换  ""
+ (NSString *)removeNil:(NSString *)str
{
    return [NSString removeNilToValue:str value:@""];
}

//nil  转换  value
+ (NSString *)removeNilToValue:(NSString *)str value:(NSString *)value
{
    if (str == nil)
    {
        return value;
    }
    return str;
}

//nil  转换  "-"
+ (NSString *)removeNilToConnect:(NSString *)str
{
    return [NSString removeNilToValue:str value:@"-"];
}

//int 转 str
+ (NSString *)intToStr:(NSInteger)num
{
    return [NSString stringWithFormat:@"%ld", num];
}

#define StrRemoveNiL(v)  ([NSString ucar_removeNil:v])
#define StrNilToValue(k,v)  ([NSString ucar_removeNilToValue:k value:v])
#define StrNilToConnect(v)  ([NSString ucar_removeNilToConnect:v])
#define IntToStr(v)  ([NSString ucar_intToStr:v])

调用方式

- (void)testRemoveNiL
{
    NSString *name = nil;
    NSString *phone = nil;
    NSString *provinceName = @"福建省";
    
    NSString *detailAddress = [NSString stringWithFormat:@"%@ %@ %@",
                               StrNilToValue(name,@"谢佳培"),
                               StrRemoveNiL(phone),
                               StrRemoveNiL(provinceName)];
    NSLog(@"收货地址:%@",detailAddress);
}

输出结果

2020-11-25 15:52:11.489857+0800 Macro[78948:5130842] 收货地址:谢佳培  福建省

消除PerformSelector警告

#define SuppressPerformSelectorLeakWarning(Stuff) \
do { \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Warc-performSelector-leaks\"") \
Stuff; \
_Pragma("clang diagnostic pop") \
} while (0)

调用方式

- (void)copyAction
{
    SuppressPerformSelectorLeakWarning(
    if ([self.formViewController canPerformAction:self.rowDescriptor.action.formSelector withSender:self.orderLabel.text])
    {
        [self.formViewController performSelector:self.rowDescriptor.action.formSelector withObject:self.orderLabel.text];
    });
}

创建单例

#define SingleH(name) +(instancetype)share##name;

#define SingleM(name)  \
static id instanceMessages;\
+(instancetype)allocWithZone:(struct _NSZone *)zone\
{\
\
static dispatch_once_t onceToken;\
\
dispatch_once(&onceToken, ^{\
\
instanceMessages = [super allocWithZone:zone];\
\
});\
\
return instanceMessages;\
}\
-(id)copy\
{\
return instanceMessages;\
}\
+(instancetype)share##name\
{\
    \
    static dispatch_once_t onceToken;\
    \
    dispatch_once(&onceToken, ^{\
        \
        instanceMessages = [[self alloc]init];\
        \
    });\
    \
    return instanceMessages;\
}

调用方式

/** 工具类单例 */
SingleH(UCARAudioTool)
SingleM(UCARAudioTool)

Demo

Demo在我的Github上,欢迎下载。
FunctionCodeBlock

参考文献

上一篇下一篇

猜你喜欢

热点阅读