NSAssert:
NSAssert()只是一個(gè)宏非洲,用于開發(fā)階段調(diào)試程序中的Bug,通過為NSAssert()傳遞條件表達(dá)式來(lái)斷定是否屬于Bug缅刽,滿足條件返回真值缕允,程序繼續(xù)運(yùn)行辉哥,如果返回假值,則拋出異常币旧,并切可以自定義異常描述丹锹。NSAssert()是這樣定義的:
#define NSAssert(condition, desc)
condition是條件表達(dá)式,值為YES或NO馏谨;desc為異常描述别渔,通常為NSString。當(dāng)conditon為YES時(shí)程序繼續(xù)運(yùn)行惧互,為NO時(shí)哎媚,則拋出帶有desc描述的異常信息。NSAssert()可以出現(xiàn)在程序的任何一個(gè)位置喊儡。
下面介紹NSParameterAssert
斷言評(píng)估一個(gè)條件拨与,如果條件為 false ,調(diào)用當(dāng)前線程的斷點(diǎn)句柄艾猜。每一個(gè)線程有它自已的斷點(diǎn)句柄买喧,它是一個(gè) NSAsserttionHandler 類的對(duì)象。當(dāng)被調(diào)用時(shí)匆赃,斷言句柄打印一個(gè)錯(cuò)誤信息淤毛,該條信息中包含了方法名、類名或函數(shù)名算柳。然后低淡,它就拋出一個(gè) NSInternalInconsistencyException 異常。
Assertions evaluate a condition and, if the condition evaluates to false, call the assertion handler for the current thread, passing it a format string and a variable number of arguments. Each thread has its own assertion handler, which is an object of classNSAssertionHandler. When invoked, an assertion handler prints an error message that includes method and class names (or the function name). It then raises anNSInternalInconsistencyException exception.
這個(gè)宏用于確認(rèn)一個(gè) Objective-C 的方法的有效性。簡(jiǎn)單提供參數(shù)作為條件就行蔗蹋。該宏評(píng)估這個(gè)參數(shù)事期,如果為 false ,它就打印一個(gè)錯(cuò)誤日志信息纸颜,該信息包含了參數(shù)并且拋出一個(gè)異常兽泣。
This macro validates a parameter for an Objective-C method. Simply provide the parameter as the condition argument. The macro evaluates the parameter and, if it is false, it logs an error message that includes the parameter and then raises an exception.
如果定義了預(yù)處理宏 NS_BLOCK_ASSERTIONS 斷言就被禁止了。所有的斷點(diǎn)宏都返回 void胁孙。
Assertions are disabled if the preprocessor macro NS_BLOCK_ASSERTIONS is defined. All assertion macros return void.