自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Foundation中的斷言處理

移動(dòng)開(kāi)發(fā) iOS
經(jīng)常在看一些第三方庫(kù)的代碼時(shí),或者自己在寫(xiě)一些基礎(chǔ)類時(shí),都會(huì)用到斷言。所以在此總結(jié)一下Objective-C中關(guān)于斷言的一些問(wèn)題。

Foundation中的斷言處理

經(jīng)常在看一些第三方庫(kù)的代碼時(shí),或者自己在寫(xiě)一些基礎(chǔ)類時(shí),都會(huì)用到斷言。所以在此總結(jié)一下Objective-C中關(guān)于斷言的一些問(wèn)題。

Foundation中定義了兩組斷言相關(guān)的宏,分別是:

  1. NSAssert / NSCAssert 
  2. NSParameterAssert / NSCParameterAssert 

這兩組宏主要在功能和語(yǔ)義上有所差別,這些區(qū)別主要有以下兩點(diǎn):

如果我們需要確保方法或函數(shù)的輸入?yún)?shù)的正確性,則應(yīng)該在方法(函數(shù))的頂部使用NSParameterAssert / NSCParameterAssert;而在其它情況下,使用NSAssert / NSCAssert。

另一個(gè)不同是介于C和Objective-C之間。NSAssert / NSParameterAssert應(yīng)該用于Objective-C的上下文(方法)中,而NSCAssert / NSCParameterAssert應(yīng)該用于C的上下文(函數(shù))中。

當(dāng)斷言失敗時(shí),通常是會(huì)拋出一個(gè)如下所示的異常:

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

Foundation為了處理斷言,專門(mén)定義了一個(gè)NSAssertionHandler來(lái)處理斷言的失敗情況。NSAssertionHandler對(duì)象是自動(dòng)創(chuàng)建的,用于處理失敗的斷言。當(dāng)斷言失敗時(shí),會(huì)傳遞一個(gè)字符串給NSAssertionHandler對(duì)象來(lái)描述失敗的原因。每個(gè)線程都有自己的NSAssertionHandler對(duì)象。當(dāng)調(diào)用時(shí),一個(gè)斷言處理器會(huì)打印包含方法和類(或函數(shù))的錯(cuò)誤消息,并引發(fā)一個(gè)NSInternalInconsistencyException異常。就像上面所看到的一樣。

我們很少直接去調(diào)用NSAssertionHandler的斷言處理方法,通常都是自動(dòng)調(diào)用的。

NSAssertionHandler提供的方法并不多,就三個(gè),如下所示:

  1. // 返回與當(dāng)前線程的NSAssertionHandler對(duì)象。 
  2. // 如果當(dāng)前線程沒(méi)有相關(guān)的斷言處理器,則該方法會(huì)創(chuàng)建一個(gè)并指定給當(dāng)前線程 
  3. + (NSAssertionHandler *)currentHandler 
  4.  
  5. // 當(dāng)NSCAssert或NSCParameterAssert斷言失敗時(shí),會(huì)調(diào)用這個(gè)方法 
  6. - (void)handleFailureInFunction:(NSString *)functionName file:(NSString *)object lineNumber:(NSInteger)fileName description:(NSString *)line, format,... 
  7.  
  8. // 當(dāng)NSAssert或NSParameterAssert斷言失敗時(shí),會(huì)調(diào)用這個(gè)方法 
  9. - (void)handleFailureInMethod:(SEL)selector object:(id)object file:(NSString *)fileName lineNumber:(NSInteger)line description:(NSString *)format, ...

另外,還定義了一個(gè)常量字符串,

  1. NSString * const NSAssertionHandlerKey; 

主要是用于在線程的threadDictionary字典中獲取或設(shè)置斷言處理器。

關(guān)于斷言,還需要注意的一點(diǎn)是在Xcode 4.2以后,在release版本中斷言是默認(rèn)關(guān)閉的,這是由宏NS_BLOCK_ASSERTIONS來(lái)處理的。也就是說(shuō),當(dāng)編譯release版本時(shí),所有的斷言調(diào)用都是無(wú)效的。

我們可以自定義一個(gè)繼承自NSAssertionHandler的斷言處理類,來(lái)實(shí)現(xiàn)一些我們自己的需求。如Mattt Thompson的NSAssertion​Handler實(shí)例一樣:

  1. @interface LoggingAssertionHandler : NSAssertionHandler 
  2. @end 
  3.  
  4. @implementation LoggingAssertionHandler 
  5.  
  6. - (void)handleFailureInMethod:(SEL)selector 
  7. object:(id)object 
  8. file:(NSString *)fileName 
  9. lineNumber:(NSInteger)line 
  10. description:(NSString *)format, ... 
  11. NSLog(@"NSAssert Failure: Method %@ for object %@ in %@#%i", NSStringFromSelector(selector), object, fileName, line); 
  12.  
  13. - (void)handleFailureInFunction:(NSString *)functionName 
  14. file:(NSString *)fileName 
  15. lineNumber:(NSInteger)line 
  16. description:(NSString *)format, ... 
  17. NSLog(@"NSCAssert Failure: Function (%@) in %@#%i", functionName, fileName, line); 
  18.  
  19. @end

上面說(shuō)過(guò),每個(gè)線程都有自己的斷言處理器。我們可以通過(guò)為線程的threadDictionary字典中的NSAssertionHandlerKey指定一個(gè)新值,來(lái)改變線程的斷言處理器。

如下代碼所示:

  1. - (BOOL)application:(UIApplication *)application 
  2. didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
  3. NSAssertionHandler *assertionHandler = [[LoggingAssertionHandler alloc] init]; 
  4. [[[NSThread currentThread] threadDictionary] setValue:assertionHandler 
  5. forKey:NSAssertionHandlerKey]; 
  6. // ... 
  7.  
  8. return YES; 
  9. }

而什么時(shí)候應(yīng)該使用斷言呢?通常我們期望程序按照我們的預(yù)期去運(yùn)行時(shí),如調(diào)用的參數(shù)為空時(shí)流程就無(wú)法繼續(xù)下去時(shí),可以使用斷言。但另一方面,我們也需要考慮,在這加斷言確實(shí)是需要的么?我們是否可以通過(guò)更多的容錯(cuò)處理來(lái)使程序正常運(yùn)行呢?

Mattt Thompson在NSAssertion​Handler中的倒數(shù)第二段說(shuō)得挺有意思,在此摘抄一下:

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.

參考

NSAssertion​Handler

NSAssertionHandler Class Reference

責(zé)任編輯:chenqingxiang 來(lái)源: 南峰子的技術(shù)博客
相關(guān)推薦

2021-07-27 06:06:34

TypeScript語(yǔ)言運(yùn)算符

2010-06-03 09:58:46

Linux 測(cè)試軟件

2023-10-29 16:18:26

Go接口

2011-07-07 09:54:01

Cocoa Core Foundation

2010-11-02 10:44:12

2009-06-17 17:09:02

Java異常Java斷言

2012-01-12 15:36:12

響應(yīng)式Web設(shè)計(jì)

2010-12-27 15:17:07

SharePoint

2019-09-23 11:07:00

PythonRedis軟件

2022-07-19 08:01:55

函數(shù)Go格式化

2017-08-28 15:21:29

異步處理回調(diào)函數(shù)異步編程

2019-09-20 08:47:57

DockerLinux軟件

2010-07-26 15:12:20

坐標(biāo)變換

2011-11-11 16:54:52

IBM云計(jì)算SmartCloudFoundation

2011-08-17 17:46:21

2024-10-12 17:08:41

2025-02-10 09:49:00

2009-01-05 09:14:17

.NETcatch性能損失

2017-11-07 13:24:38

Pythontime模塊datetime

2024-07-29 10:46:50

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)