iOS使用宏定义函数和代码块
今天在开发过程中碰到一个问题:就是父类中要向外发送通知,然后子类中或者其他类中来接收它。当然一般是把它写到类方法中去,但是有个问题,就是如果调用的类不是它的子类,就不能直接调用,当然也可以采用静态方法实现,我这里主要是想用宏定义来实现,下面我分别介绍使用宏定义函数和定义代码块的方式进行,废话不多说了,直接上代码:
- 使用宏定义函数实现
//定义
#define SendNotification @"SendNotification"
#define sendMessage(msg) \
({\
dispatch_async(dispatch_get_main_queue(), ^{\
NSNotificationCenter* notificationCenter = [NSNotificationCenter defaultCenter];\
[notificationCenter postNotificationName:SendNotification object:nil userInfo:@{
@"msg":msg}];\
});\
})