標籤:調試宏 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