NSAssert和NSParameterAssert

2016.05.05 18:34*?字數 861?閱讀 5127評論 0

NSAssert和NSParameterAssert在開發環境中經常被使用,調試和驗證代碼參數的完整性,斷言為真,則表明程序運行正常,而斷言為假,則意味著它已經在代碼中發現了意料之外的錯誤。xCode中的斷言在Debug模式默認是開啟的,Realse版本中是禁用的.

基礎斷言

基礎類庫中了兩種斷言,NSAssert和NSParameterAssert是OC斷言,NSCAssert和NSCParameterAssert是C語言斷言。先來看一下NSAssert定義:
<pre><code>The NSAssert macro evaluates the condition and serves as a front end to the assertion handler. Each thread has its own assertion handler, which is an object of class NSAssertionHandler. When invoked, an assertion handler prints an error message that includes the method and class names (or the function name). It then raises an NSInternalInconsistencyException exception. If condition evaluates to NO, the macro invokes handleFailureInMethod:object:file:lineNumber:description: on the assertion handler for the current thread, passing desc as the description string. This macro should be used only within Objective-C methods. Assertions are disabled if the preprocessor macro NS_BLOCK_ASSERTIONS is defined. **Important:Important** Do not call functions with side effects in the condition parameter of this macro. The condition parameter is not evaluated when assertions are disabled, so if you call functions with side effects, those functions may never get called when you build the project in a non-debug configuration. **Note:Note** Not all release configurations disable assertions by default.</code></pre>
NSParameterAssert的定義:
<pre>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 class NSAssertionHandler. When invoked, an assertion handler prints an error message that includes method and class names (or the function name). It then raises an NSInternalInconsistencyException exception. 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. Assertions are disabled if the preprocessor macro NS_BLOCK_ASSERTIONS is defined. All assertion macros return void. **Important:Important** Do not call functions with side effects in the condition parameter of this macro. The condition parameter is not evaluated when assertions are disabled, so if you call functions with side effects, those functions may never get called when you build the project in a non-debug configuration. **Note:Note** Not all release configurations disable assertions by default.</pre>
兩者的定義類似,大概意思就是如果是false就會調用當前線程Assertion Hanlder進行處理,非Debug模式下可能所有的斷言都不會調用,最后一句很重要,并不是所有的發布配置會禁用斷言,如果想看斷言是否禁用,需要看一下設置:

Snip20160505_1.png

簡單測試:
<pre><code>`
NSString *result=@"中山郎";
NSInteger count=10;
NSAssert(count>10, @"總數必須大于10");
NSLog(@"斷言執行之后");

?

</code></pre> 崩潰信息: <pre><code>
** FECategory[23811:248235] *** Assertion failure in -[ViewController setupAssert], /ViewController.m:45**
** FECategory[23811:248235] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '****總數必須大于****10'**`</code></pre>

NSAssertionHandler

NSAssert異常處理的時候默認是NSAssertionHandler處理的,不過我們可以自定自己的Handler,實現兩個方法:
<pre><code>`

  • (void)handleFailureInMethod:(SEL)selector object:(id)object file:(NSString *)fileName lineNumber:(NSInteger)line description:(nullable NSString *)format,... NS_FORMAT_FUNCTION(5,6);
  • (void)handleFailureInFunction:(NSString *)functionName file:(NSString *)fileName lineNumber:(NSInteger)line description:(nullable NSString *)format,... NS_FORMAT_FUNCTION(4,5);`</code></pre>

handleFailureInMethod處理OC方法中的斷言,handleFailureInFunction處理C函數中的斷言
自定義繼承自NSAssertionHandler的類FEAssertionHandler:
<pre><code>`
@implementation FEAssertionHandler

-(void)handleFailureInMethod:(SEL)selector object:(id)object file:(NSString *)fileName lineNumber:(NSInteger)line description:(NSString *)format, ...{
NSLog(@"FlyElephant-FEAssertionHandler: Method %@ for object %@ in %@--line:%li", NSStringFromSelector(selector), object, fileName, (long)line);
}

-(void)handleFailureInFunction:(NSString *)functionName file:(NSString *)fileName lineNumber:(NSInteger)line description:(NSString *)format, ...{
NSLog(@"FlyElephant-FEAssertionHandler:Function (%@) in %@--line:%li", functionName, fileName, (long)line);
}

@end
</code></pre> AppDelegate中設置斷言處理: <pre><code>

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    FEAssertionHandler *hanlder=[[FEAssertionHandler alloc]init];
    [[[NSThread currentThread] threadDictionary] setValue:hanlder forKey:NSAssertionHandlerKey];
    return YES;
    }

轉載于:https://www.cnblogs.com/sundaysgarden/p/10354170.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/249686.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/249686.shtml
英文地址,請注明出處:http://en.pswp.cn/news/249686.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【PAT】B1070 結繩(25 分)

此題太給其他25分的題丟人了&#xff0c;只值15分 注意要求最終結果最長&#xff0c;而且向下取整 #include<stdio.h> #include<algorithm> using namespace std; float arr[10005]; int main(){int N;scanf("%d",&N);for(int i0;i<N;i)//輸入數據…

Java代碼實現負載均衡五種算法

前言&#xff1a; 負載均衡是為了解決并發情況下&#xff0c;多個請求訪問&#xff0c;把請求通過提前約定好的規則轉發給各個server。其中有好幾個種經典的算法。在用java代碼編寫這幾種算法之前&#xff0c;先來了解一下負載均衡這個概念。 1.概念 負載&#xff0c;從字面…

使用Nodejs發送郵件

嘗試用了Nodemailer來發送郵件&#xff0c;結果成功了&#xff0c;雖然是相對比較簡單的&#xff0c;但還是記錄一下吧。 Nodemailer 是 Node.js 應用程序的一個模塊&#xff0c;可以方便地發送電子郵件。 使用 # 初始化 pageage.json 文件 $ npm init # 安裝依賴 $ npm ins…

HTTP同源策略

同源策略是web安全策略中的一種&#xff0c;非常重要。 同源策略明確規定&#xff1a;不同域的客戶端在沒有明確授權的情況下&#xff0c;不能讀寫對方的資源。 簡單說來就是web瀏覽器允許第一個頁面的腳本訪問訪問第二個頁面的數據&#xff0c;但是也只有在兩個頁面有相同的…

Spring Cloud 微服務架構

一、分布式服務框架的發展 1.1 第一代服務框架   代表&#xff1a;Dubbo(Java)、Orleans(.Net)等 特點&#xff1a;和語言綁定緊密 1.2 第二代服務框架   代表&#xff1a;Spring Cloud等 現狀&#xff1a;適合混合式開發&#xff08;例如借助Steeltoe OSS可以讓ASP.Ne…

JZOJ 4421. aplusb

4421. aplusb Time Limits: 1000 ms Memory Limits: 524288 KB Detailed Limits Goto ProblemSetDescription SillyHook要給小朋友出題了&#xff0c;他想&#xff0c;對于初學者&#xff0c;第一題肯定是ab 啊&#xff0c;但當他出完數據后神奇地發現.in不見了&#xff0c…

跨域資源共享CORS詳解

最近深入了解了CORS的相關東西&#xff0c;覺得阮一峰老師的文章寫得最詳細易懂了&#xff0c;所有轉載作為學習筆記。 原文地址&#xff1a;跨域資源共享 CORS 詳解 CORS是W3C的一個標準&#xff0c;全稱是跨域資源共享&#xff08;Cross-origin resource sharing&#xff0…

計算機網絡(十),HTTP的關鍵問題

目錄 1.在瀏覽器地址欄鍵入URL&#xff0c;按下回車之后經歷的流程 2.HTTP狀態碼 3.GET請求和POST請求的區別 4.Cookie和Session的區別 5.IPV4和IPV6 十、HTTP的關鍵問題 1.在瀏覽器地址欄鍵入URL&#xff0c;按下回車之后經歷的流程 &#xff08;1&#xff09;DNS解析 &#x…

云技術

云技術是指在廣域網或局域網內將硬件、軟件、網絡等系列資源統一起來&#xff0c;實現數據的計算、儲存、處理和共享的一種托管技術。

vue中 mock使用教程

//mock/index.js import Mock from mockjs //引入mockjs&#xff0c;npm已安裝 import { Random,toJSONSchema } from mockjs // 引入random對象,隨機生成數據的對象&#xff0c;&#xff08;與占位符一樣&#xff09; Mock.setup({timeout:1000 //設置請求延時時間 }) const …

前端開發掌握nginx常用功能之rewrite

上一篇博文對nginx最常用功能的server及location的匹配規則進行了講解&#xff0c;這也是nginx實現控制訪問和反向代理的基礎。掌握請求的匹配規則算是對nginx有了入門&#xff0c;但是這些往往還是不能滿足實際的需求場景&#xff0c;例如請求url重寫、重定向等等&#xff0c;…

vue2.0腳手架的webpack 配置文件分析

前言 作為 Vue 的使用者我們對于 vue-cli 都很熟悉&#xff0c;但是對它的 webpack 配置我們可能關注甚少&#xff0c;今天我們為大家帶來 vue-cli#2.0 的 webpack 配置分析 vue-cli 的簡介、安裝我們不在這里贅述&#xff0c;對它還不熟悉的同學可以直接訪問 vue-cli 查看 …

一個可供中小團隊參考的微服務架構技術棧

一個可供中小團隊參考的微服務架構技術棧

WinSxS文件夾瘦身

WinSxS文件夾瘦身2014-5-8 18:03:32來源&#xff1a;IT之家作者&#xff1a;阿象責編&#xff1a;阿象 評論&#xff1a;27剛剛&#xff0c;我們分享了如何用DISM管理工具查看Win8.1 WinSxS文件夾實際大小。對于WinSxS文件夾&#xff0c;幾乎每個Windows愛好者都認識到其重要性…

bcrypt的簡單使用

前段時間在搗鼓個人項目的時候用到了nodejs做服務端&#xff0c;發現使用加密的方法和之前常用的加密方式不太一致&#xff0c;下面以demo的形式總結一下bcrypt對密碼進行加密的方法。 一、簡介 Bcrypt簡介&#xff1a; bcrypt是一種跨平臺的文件加密工具。bcrypt 使用的是布…

盒子居中

1、未脫標 margin&#xff1a;0 auto&#xff1b; 2、脫標&#xff08;absolute、fixed&#xff09; left&#xff1a;50%&#xff1b; margin-left&#xff1a;width/2&#xff1b; 轉載于:https://www.cnblogs.com/liujianing/p/10356984.html

織夢無子欄目時禁止調用同級欄目

1. 修改文件 \include\taglib\channel.lib.php 把代碼 if($typeson && $reid!0 && $totalRow0) 改為 if($typeson && $reid!0 && $totalRow0 && $noself) 2. 使用channel標簽時添加noself屬性 {dede:channel noselfyes} {/dede:channe…

nodejs實現文件上傳

前段時間在做個人項目的時候&#xff0c;用到了nodejs服務端上傳文件&#xff0c;現在回頭把這個小結一下&#xff0c;作為記錄。 本人上傳文件時是基于express的multiparty&#xff0c;當然也可以使用connect-multiparty中間件實現&#xff0c;但官方似乎不推薦使用connect-m…

python騰訊語音合成

一、騰訊語音合成介紹 騰訊云語音合成技術&#xff08;TTS&#xff09;可以將任意文本轉化為語音&#xff0c;實現讓機器和應用張口說話。 騰訊TTS技術可以應用到很多場景&#xff0c;比如&#xff0c;移動APP語音播報新聞&#xff1b;智能設備語音提醒&#xff1b;依靠網上現有…