IOS uses macros to define functions and blocks of code

Source: Internet
Author: User
Tags define function

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.