加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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];
断言(self.formatter!= nil);

要么 :

- (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/
> http://lists.apple.com/archives/objc-language/2006/Sep/msg00000.html
> D programming language有DbC,这里的Michel Fortins尝试的是DbC的相同款式,但是对于Objective C:http://michelf.com/weblog/2010/dobjc-preliminary-design/

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读