@這裏給介紹幾個系統給我們,很方便進行程序調試,定位錯誤的宏
我們寫程序時,對於不放心或容易報錯的地方,可以加上這個代碼
#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"]'