iOS 使用宏定義函數和代碼塊

來源:互聯網
上載者:User

標籤:cat   nil   父類   子類   介紹   gets   定義函數   就是   isp   

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}];    });})//使用sendMessage(@"發個訊息試試");//有返回的宏函數定義#define getSum(a,b) ({(a+b);})//使用double sum = getSum(M_PI,M_E);
  • 使用宏定義代碼塊實現
//定義#define SendNotification @"SendNotification"#define sendMessage(msg) ^(){    dispatch_async(dispatch_get_main_queue(), ^{        NSNotificationCenter* notificationCenter = [NSNotificationCenter defaultCenter];        [notificationCenter postNotificationName:SendNotification object:nil userInfo:@{@"msg":msg}];    });}()//使用sendMessage(@"發個訊息試試");//有返回的宏代碼塊定義#define getSum(a,b)^(){    return a+b;}()//使用double sum = getSum(M_PI,M_E);
  • 寫在最後,當時寫的時候,想到了使用宏定義的方式,但是在網上找了一圈沒有找到怎麼使用宏來定義代碼塊和函數,於是自己通過嘗試實現了,所以在這裡Mark一下,希望能夠幫到遇到同樣問題的人,也為了以後自己忘了能夠查到。

iOS 使用宏定義函數和代碼塊

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.