iOS断言-抛异常
2015-12-28 11:56
621 查看
自定义描述异常信息的desc就是所谓的抛异常
注意:assert是一个宏,只在debug版本中起作用,在release版本中,该语句是不起任何作用的。
示例一:给Label属性赋值不能为空,添加断言,如果为空,则直接crash,抛异常
注意:assert是一个宏,只在debug版本中起作用,在release版本中,该语句是不起任何作用的。
示例一:给Label属性赋值不能为空,添加断言,如果为空,则直接crash,抛异常
<pre name="code" class="objc">#import "MainViewController.h" @interface MainViewController () @property(nonatomic,weak)UILabel *lb_title; @end @implementation MainViewController - (void)viewDidLoad { [super viewDidLoad]; UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(10, 200, 100, 30)]; [self.view addSubview:lable]; self.lb_title = lable; self.lb_title.text = @""; } - (void)setLb_title:(UILabel *)lb_title{ NSAssert(lb_title.text !=nil, @"标题不能为空"); _lb_title = lb_title; }
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { BOOL isOn = NO; NSAssert(isOn, @"如果表达式是假,直接报错"); }
利用宏自定义断言 #define DXAssertNil(a,b,...) NSAssert((a)==nil,(b)) #define DXAssertNotNil(a,b,...) NSAssert((a)!=nil,(b)) #define DXAssertTrue(a,b,...) NSAssert((a),(b)) #define DXAssertEquals(a,b,c,...) NSAssert((a==b),(c)) #define DXAssertNotEquals(a,b,c,...) NSAssert((a!=b),(c))
相关文章推荐
- iOS界面淡进淡出的效果
- iOS开发证书介绍
- iOS正则表达式
- IOS版添加phonegap--融云即时通讯插件教程
- iOS 项目中常见错误分析
- iOS获取当前时间错误
- iOS开发中善用日志记录工具
- iOS推送之远程推送(iOS Notification Of Remote Notification)
- iOS实现滑动返回
- iOS推送之本地推送(iOS Notification Of Local Notification)
- iOS 单元测试
- iOS输入框怎样避免键盘的遮挡
- iOS开发之音频口通信-通过方波来收发数据
- iOS GCD 队列 线程
- iOS--导航栏属性设置的介绍
- iOS CocoaPods详解之 Analyzing dependencies
- [ios][opengles]opengles纹理贴图
- 如何解iOS系统中系统库的dyld缓存
- iOS 开发中遇到的小问题
- 一些iOS笔试题目