iOS uses macros to define functions and blocks of code
Today, in the development process, there is a problem: it is the parent class to send out notifications, and then in the subclass or other class to receive it. Of course, it is generally written into the class method, but there is a problem, that is, if the calling class is not its subclass, it can not be directly invoked, of course, it is also possible to implement the static method, I am here mainly to use the macro definition to implement, the following I introduce the use of macro definition function and define code block way to do, Directly on the code:
- Using macros to define function implementations
/ /definition #define sendnotification @ "SendNotification" #define sendMessage (msg) ({Dispatch_async (dispatch_get_main _queue (), ^{nsnotificationcenter* notificationcenter = [Nsnotificationcenter defaultcenter]; [Notificationcenter postnotificationname:sendnotification object:nil UserInfo:@{@ "MSG": Msg}]; });}) //use SendMessage (@ "send a message to try"); #define getsum (A, B) ({(a+b);}) //use double sum = getsum (m_pi,m_e);
- Using a macro to define a code block implementation
//Definition #define sendnotification @ "SendNotification"#define SendMessage (msg) ^ () {Dispatch_async ( Dispatch_get_main_queue (), ^{nsnotificationcenter* notificationcenter = [Nsnotificationcenter defaultCenter]; [Notificationcenter postnotificationname:sendnotification object:nil userinfo:@{@ "msg": Msg}];}); ()//Use SendMessage (@ "send a message to try"); There is a return macro code block definition #define Getsum (A, B) ^ () { return a+b;} ()//use double sum = getsum (m_pi,m_e);
- Write at the end, when writing, thought of the use of macro definition, but in the Internet to find a circle did not find how to use the macro to define the code block and function, so I tried to achieve, so here mark, I hope to help people who meet the same problem, but also for the future I forgot to be able to find.
IOS uses macros to define functions and blocks of code