NSAssert可用来对结果进行断言,如下用于断言UIButton的title不为空:
- (IBAction)actionBtnClicked:(UIButton *)sender {
NSAssert(sender.titleLabel.text, @"1: sender.titleLable.text not nil");
NSAssert(![sender.titleLabel.text isEqualToString:@""], @"2: sender.titleLable.text not nil");
NSAssert(sender.titleLabel.text == nil, @"3: sender.titleLable.text not nil");
NSLog(@"sender.name : %@", sender.titleLabel.text);
}
执行结果:
第三个断言会失败,报出下边的错误:
区分Debug和Release
有些时候,我们仅仅想在Debug版本中使用NSAssert功能,而在Release版本中关闭NSAssert功能,则可以在Building Settings->Preprocessor Macros中,设置Release版本的NS_BLOCK_ASSERTIONS,禁止断言检查即可:
在Edit Scheme->Run->Build Configuration中,更改编译版本为Release,运行程序,NSAssert不会报错。
| 更改编译版本 | Release版本下NSAssert不会报错 |
|---|---|

本文介绍了如何使用NSAssert来断言UIButton的标题不为空,并通过示例代码解释了不同断言条件的效果。此外还讨论了如何在Debug和Release版本中配置NSAssert行为。

1741

被折叠的 条评论
为什么被折叠?



