【转】使用断言NSAssert()调试程序错误
#define NSAssert(condition, desc)
condition是条件表达式,值为YES或NO;desc为异常描述,通常为NSString。当conditon为YES时程序继续运行,为NO时,则抛出带有desc描述的异常信息。NSAssert()可以出现在程序的任何一个位置。具体事例如下:
生成一个LotteryEntry对象时,传入的NSDate不能为nil,加入NSAssert()判断。对象初始化源码如下:
- (id)initWithEntryDate:(NSDate *)theDate
{
self =
[super init];
if (self)
{
NSAssert(theDate
!= nil, @"Argument must be
non-nil");
entryDate =
theDate;
firstNumber =
(int)random()
% 100 + 1;
secondNumber =
(int)random()
% 100 + 1;
}
return self;
}
接下来则是生成对象时传入一个值为nil的NSDate,看断言是否运行。
LotteryEntry *nilEntry =
[[LotteryEntry alloc] initWithEntryDate:nil];
断言效果如下:
2013-01-17 20:49:12.486 lottery[3951:303] *** Terminating app
due to uncaught exception 'NSInternalInconsistencyException',
reason: 'Argument must be non-nil'
*** First throw call stack:
(
0 CoreFoundation
0x00007fff90c590a6 __exceptionPreprocess +
198
1 libobjc.A.dylib
0x00007fff8fd2a3f0 objc_exception_throw + 43
2 CoreFoundation
0x00007fff90c58ee8 +[NSException
raise:format:arguments:] + 104
3 Foundation
0x00007fff88dae6a2 -[NSAssertionHandler
handleFailureInMethod:object:file:lineNumber:description:] +
189
4 lottery
0x0000000100001929 -[LotteryEntry
initWithEntryDate:] + 249
5 lottery
0x0000000100001794 main + 932
6 libdyld.dylib
0x00007fff8d83f7e1 start + 0
)
libc++abi.dylib: terminate called throwing an exception
【转】使用断言NSAssert()调试程序错误的更多相关文章
- 使用断言NSAssert()调试程序错误
NSAssert()只是一个宏,用于开发阶段调试程序中的Bug,通过为NSAssert()传递条件表达式来断定是否属于Bug,满足条件返回真值,程序继续运行,如果返回假值,则抛出异常,并切可以自定义异 ...
- iOS-----程序异常处理----- 断言NSAssert()和NSParameterAssert区别和用处
NSAssert和assert是断言,主要的差别是assert在断言失败的时候只是简单的终止程序,而NSAssert会报告出错误信息并且打印出来.所以尽管的使用NSAssert,可以不去使用asser ...
- 断言NSAssert的使用
1. NSAssert 断言(NSAssert)是一个宏,在开发过程中使用NSAssert可以及时发现程序中的问题. NSAssert声明如下: #define NSAssert(condition, ...
- 断言--NSAssert
NSAssert()是一个宏,用于开发阶段调试程序中的Bug,通过为NSAssert()传递条件表达式来断定是否属于Bug,满足条件返回真值,程序继续运行,如果返回假值,则抛出异常,并切可以自定义异常 ...
- 学会用core dump调试程序错误
最来在项目中遇到大型程序出现SIGSEGV ,一直不知道用core dump工具来调试程序,花了近一周的时间,才定位问题,老大很生气,后果很严重,呵呵,事后仔细学习了这块的知识,了解一点core du ...
- iOS 笔记
1. 使用断言NSAssert()调试程序错误 NSAssert()只是一个宏,用于开发阶段调试程序中的Bug,通过为NSAssert()传递条件表达式来断定是否属于Bug,满足条件返回真值,程序继续 ...
- iOS 10开发NSAssert(断言)的使用
断言(NSAssert)的使用 字数1055 阅读3270 评论3 喜欢30 NSAssert()是一个宏,用于开发阶段调试程序中的Bug,通过为NSAssert()传递条件表达式来断定是否属于Bug ...
- Swift开发第一篇——异常处理及断言
本篇分两部分: 1.错误和异常处理 2.Swift 中的断言 1.错误和异常处理 在 OC 开发中,我们通常会将 error 置为 nil NSError *error; BOOL success = ...
- 异常:NSException和NSAssert的简单使用
//断言 - (void)NSAssert_Test:(NSString *)string{ NSAssert(string == nil, @"string == kong or nil& ...
随机推荐
- python 前向引用
即函数调用在函数定义之前 可以这样 def bbb(): print('this is b') aaa() def aaa(): print('this is a') bbb() ---------& ...
- hdu 4115 Eliminate the Conflict ( 2-sat )
Eliminate the Conflict Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- css div 垂直居中
参考:http://css-tricks.com/centering-in-the-unknown/ <style> .valign { font-size: 0px;/* clear s ...
- halcon,C# 学习
Halcon学习之一:查询图像参数 1.get_grayval ( Image : : Row, Column : Grayval ) 计算Image图像中坐标为(Row,Column)的点的灰度值G ...
- IAP内购
IAPHelper.h // // IAPHelper.h // airplay // // Created by apple on 13-10-23. // Copyright (c) 2013年 ...
- MFC中 Invalidate() , InvalidateRect() , UpdateWindow(), Redrawwindow() 区别
1. void Invalidate( BOOL bErase = TRUE ); 该函数的作用是使整个窗口客户区无效.窗口的客户区无效意味着需要重绘,例如,如果一个被其它窗口遮住的窗口变成了前台窗口 ...
- 微信get post请求到微信服务器 模版 素材操作
1:素材管理 官方文档 package org.konghao.weixin.media; import java.io.File; import java.io.IOException; impor ...
- TCP/IP TIME_WAIT状态原理
原文转载:http://elf8848.iteye.com/blog/1739571 IME_WAIT状态原理 ---------------------------- 通信双方建立TCP连接后,主动 ...
- 使用visual studio 2013 快速搭建phonegap开发环境
前一段时间开发了一款简单的Phonegap应用,遇到了很多坑,其中有一个坑就是在搭建开发环境上.由于Phonegap 2.x 与3.x 区别比较大,导致了开发环境也有所不同.2.x 是这样的http: ...
- android一些系统相关的东西
添加快捷方式和删除快捷方式: private void addShortcut() { Intent shortcut = new Intent( "com.android.launcher ...