IOS總結_#define宏定義在IOS開發中的具體使用

來源:互聯網
上載者:User

標籤:宏

以下內容只是拋磚引玉,希望大家多提意見,多改錯誤,太簡單的宏使用方法就不做解釋了,來點擴充的

//沒用#define前是這樣的,每個ViewController都要寫這麼一段重複的代碼 寫多了就煩了

    /***********************處理前*****************************/

    UILabel *titleLabel = [[UILabel alloc] init];

    titleLabel.backgroundColor  = [UIColor clearColor];

    titleLabel.textColor        = [UIColor redColor];

    titleLabel.text             = @"導覽列標題";

    titleLabel.font=[UIFont fontWithName:nil size:17.0f];

    [titleLabel sizeToFit];

    self.navigationItem.titleView = titleLabel;


//在全域設定檔(config.h)中定義了一個宏

#define SET_NAV_TITLE(_TITLE) ({\

UILabel *titleLabel = [[UILabel alloc] init];\

titleLabel.backgroundColor  = [UIColor clearColor];\

titleLabel.textColor        = [UIColor redColor];\

titleLabel.text             = _TITLE;\

titleLabel.font=[UIFont fontWithName:nil size:17.0f];\

[titleLabel sizeToFit];\

self.navigationItem.titleView = titleLabel;\

})


    //定義了宏後就這麼一句了

    /***********************處理後*****************************/

    SET_NAV_TITLE(@"導覽列標題");

    

    //向大家請教個問題,怎麼向宏裡面傳一個執行個體對象進去,可不可以實現


最後給大家介紹一個好玩的,試調時很好用,把下面一段代碼複製到.pch中,你列印的時候就能看到效果了


//一個很進階的 NSLog

#define NSLog(format, ...) do {      \

fprintf(stderr, "<%s : %d> %s\n",                                           \

[[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String],  \

__LINE__, __func__);                                                        \

(NSLog)((format), ##__VA_ARGS__);                                           \

fprintf(stderr, "-------------------------------完美分割線---嘿嘿----------------------------\n");          \

} while (0)


IOS總結_#define宏定義在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.