objective-c – 为什么在项目中使用’assert’? (以及为何多次
发布时间:2020-12-16 03:35:24 所属栏目:百科 来源:网络整理
导读:我正在阅读示例代码ListAdder,并且在变量之后有很多断言,或者几乎在每个方法中使用,例如: self.formatter = [[[NSNumberFormatter alloc] init] autorelease]; 断言(self.formatter!= nil); 要么 : - (UITableViewCell *)tableView:(UITableView *)tv cel
我正在阅读示例代码ListAdder,并且在变量之后有很多断言,或者几乎在每个方法中使用,例如:
self.formatter = [[[NSNumberFormatter alloc] init] autorelease]; 要么 : - (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath { #pragma unused(tv) #pragma unused(indexPath) UITableViewCell * cell; assert(tv == self.tableView); assert(indexPath != NULL); assert(indexPath.section < kListAdderSectionIndexCount); assert(indexPath.row < ((indexPath.section == kListAdderSectionIndexNumbers) ? [self.numbers count] : 1)); 我在想,有什么意义呢? 谢谢 解决方法
它是
Design by Contract或
DbC的实现.
目标C对DbC的前置条件,后置条件和不变量没有原生支持,但特别是后置和前置条件可以很好地实现宏. 以下是在Objective C中实现DbC的一些其他方法: > http://www.roard.com/contracts/ (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |