NSAssert NSCAssert NSParameterAssert

來源:互聯網
上載者:User

標籤:調試宏   nsassert   

      @這裡給介紹幾個系統給我們,很方便進行程式調試,定位錯誤的宏

         我們寫程式時,對於不放心或容易報錯的地方,可以加上這個代碼

#define NSAssert(condition, desc, ...)#define NSCAssert(condition, desc, ...)第一個參數為一個條件判斷,如果為假,則拋出異常,顯示第二個參數所描述的資訊(定義為自己能看的很明白的錯誤提示資訊)。例如:    NSString *test = @"HMT";    NSAssert([test isEqualToString:@"TMH"], @"your name is error");    NSCAssert([test isEqualToString:@"TMH"], @"your name is error");1.在debug模式下運行,會終止程式,並拋出如下異常:2014-09-11 18:26:02.008 DemoTest[42915:60b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'your name is error'2.在release模式下運行,不終止程式不拋出異常。@小心使用NSAssert,可以看到它的定義中出現了一個self,有可能在你的block中你會發現你明明沒有self的strong引用,但是仍然出現了循環參考。就看看你是否使用了NSAssert,這個宏被展開之後持有了self,那麼有可能就會出現引用不釋放的問題。而使用NSCassert,就不會有這樣的問題了。因為它定義使用的handleFailureInFunction函數,並沒有self引用。#define NSParameterAssert(condition)    程式在相應位置設定的條件不滿足的時候拋出來,用NSParameterAssert讓程式crash到相應位置:2014-09-11 18:26:43.524 DemoTest[42982:60b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: [test isEqualToString:@"TMH"]'



NSAssert NSCAssert NSParameterAssert

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.