#define NSAssert(condition, desc)
condition是条件表达式,值为YES或NO;desc为异常描述,通常为NSString。当conditon为YES时程序继续运行,为NO时,则抛出带有desc描述的异常信息。NSAssert()可以出现在程序的任何一个位置。具体事例如下:
这是一个循环把数组中view加到self上的方法,数组中参数必须是view类型
- (void)batchAddSubviews:(NSArray *)subviews
{
for (id obj in subviews) {
if ([obj isKindOfClass:[UIView class]]) {
[self addSubview:(UIView *)obj];
} else {
NSAssert(NO, @"参数必须全是UIView类型的对象");
}
}
}
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
_imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"classification_loadFailure_bg@3x"]];
_messageLabel = [[UILabel alloc] init];
_messageLabel.textAlignment = NSTextAlignmentCenter;
_messageLabel.numberOfLines = 0;
[self setupMessage];
_retryButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_retryButton setTitle:@"重新加载" forState:UIControlStateNormal];
[_retryButton setTitleColor:RGBCOLOR(100, 100, 100) forState:UIControlStateNormal];
_retryButton.titleLabel.font = [UIFont systemFontOfSize:14];
_retryButton.layer.cornerRadius = 5;
_retryButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
_retryButton.layer.borderWidth = 0.5;
[_retryButton addTarget:self action:@selector(clickRetryButton:) forControlEvents:UIControlEventTouchUpInside];
[self batchAddSubviews:@[_imageView, _messageLabel, _retryButton, @(2)]];
[self setupLayout];
}
return self;
}
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '参数必须全是UIView类型的对象'
*** First throw call stack:
(0x185c20f48 0x19a7d3f80 0x185c20e18 0x186b14a1c 0x1003f114c 0x1005598c0 0x100559204 0x10017ab00 0x10017a894 0x10017a848 0x18b1b4098 0x18b1b3c9c 0x1007d1e10 0x1007d1b70 0x1002fb7bc 0x1002fc870 0x18b229324 0x18b457acc 0x18b45be0c 0x18b458f50 0x18fa3f7c4 0x18fa3fb44 0x185bd8544 0x185bd7fd8 0x185bd5cd8 0x185b04ca0 0x18b2221c8 0x18b21cffc 0x100200d0c 0x19b0228b8)
libc++abi.dylib: terminating with uncaught exception of type NSException