您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發基礎

Foundation 斷言機制解析:NSAssert 與 NSCAssert 的差別與自訂處理器

編輯:iOS 開發基礎

不管是讀第三方庫的程式碼,還是自己寫基礎類別,都常會用到斷言。這裡整理一下 Objective-C 中關於斷言的一些要點。

Foundation 定義了兩組斷言相關的巨集:

NSAssert / NSCAssert\nNSParameterAssert / NSCParameterAssert

這兩組主要在功能與語義上有差別,具體有兩點:

  1. 若要確保方法或函數的輸入參數正確,應在方法(函數)頂部使用 NSParameterAssert / NSCParameterAssert;其他情況則用 NSAssert / NSCAssert。

  2. 另一個差別介於 C 與 Objective-C 之間:NSAssert / NSParameterAssert 用於 Objective-C 上下文(方法)中,而 NSCAssert / NSCParameterAssert 用於 C 上下文(函數)中。

斷言失敗時,通常會拋出這樣的異常:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'true is not equal to false'

為處理斷言,Foundation 專門定義了 NSAssertionHandler。這個物件會自動建立,用於處理失敗的斷言:當斷言失敗時,系統會傳一個描述失敗原因的字串給它。每個線程都有自己的 NSAssertionHandler 物件。被呼叫時,斷言處理器會印出包含方法與類別(或函數)的錯誤訊息,並引發 NSInternalInconsistencyException 異常——就像上面看到的那樣。

我們很少直接呼叫 NSAssertionHandler 的處理方法,通常都是系統自動觸發。它提供的就只有三個方法:

// 返回與當前線程關聯的 NSAssertionHandler 物件;\n// 若當前線程沒有相關處理器,則建立一個並指定給當前線程\n+ (NSAssertionHandler *)currentHandler\n\n// 當 NSCAssert 或 NSCParameterAssert 失敗時呼叫\n- (void)handleFailureInFunction:(NSString *)functionName\n                           file:(NSString *)fileName\n                     lineNumber:(NSInteger)line\n                    description:(NSString *)format, ...\n\n// 當 NSAssert 或 NSParameterAssert 失敗時呼叫\n- (void)handleFailureInMethod:(SEL)selector\n                       object:(id)object\n                         file:(NSString *)fileName\n                   lineNumber:(NSInteger)line\n                  description:(NSString *)format, ...

此外還定義了一個常量字串:

NSString * const NSAssertionHandlerKey;

主要用於在線程的 threadDictionary 中取得或設定斷言處理器。

Release 版本中的行為

要注意的是,從 Xcode 4.2 之後,release 版本中斷言預設是關閉的,這由巨集 NS_BLOCK_ASSERTIONS 控制。也就是說,編譯 release 版本時,所有斷言呼叫都會失效。

自訂斷言處理器

我們可以繼承 NSAssertionHandler 來滿足自己的需求,例如 Mattt Thompson 的 LoggingAssertionHandler:

@interface LoggingAssertionHandler : NSAssertionHandler\n@end\n\n@implementation LoggingAssertionHandler\n- (void)handleFailureInMethod:(SEL)selector\n                       object:(id)object\n                         file:(NSString *)fileName\n                   lineNumber:(NSInteger)line\n                  description:(NSString *)format, ... {\n    NSLog(@"NSAssert Failure: Method %@ for object %@ in %@#%i", NSStringFromSelector(selector), object, fileName, line);\n}\n\n- (void)handleFailureInFunction:(NSString *)functionName\n                           file:(NSString *)fileName\n                     lineNumber:(NSInteger)line\n                    description:(NSString *)format, ... {\n    NSLog(@"NSCAssert Failure: Function (%@) in %@#%i", functionName, fileName, line);\n}\n@end

既然每個線程都有自己的斷言處理器,那麼只要為線程 threadDictionary 中的 NSAssertionHandlerKey 指定新值,就能替換掉它:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {\n    NSAssertionHandler *assertionHandler = [[LoggingAssertionHandler alloc] init];\n    [[[NSThread currentThread] threadDictionary] setValue:assertionHandler forKey:NSAssertionHandlerKey];\n    // ...\n    return YES;\n}

什麼時候該用斷言

通常是我們期望程式依預期運行時使用,例如傳入的參數為空、流程就無法繼續的情況。但另一方面也得捫心自問:在這裡加斷言真的必要嗎?是否可以透過更多容錯處理讓程式正常運行?

Mattt Thompson 在介紹 NSAssertionHandler 時有一段話頗有意思,摘錄如下:

But if we look deeper into NSAssertionHandler—and indeed, into our own hearts, there are lessons to be learned about our capacity for kindness and compassion; about our ability to forgive others, and to recover from our own missteps. We can't be right all of the time. We all make mistakes. By accepting limitations in ourselves and others, only then are we able to grow as individuals.

參考

  1. NSAssertionHandler

  2. NSAssertionHandler Class Reference

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved