小编use*_*111的帖子

强制施展真的很糟糕,应该总是避免吗?

我开始使用swiftLint并注意到Swift的最佳实践之一是避免使用强制转换.但是在处理tableView,collectionView for cells时我经常使用它:

let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellID, forIndexPath: indexPath) as! MyOffersViewCell
Run Code Online (Sandbox Code Playgroud)

如果这不是最好的做法,那么处理这个问题的正确方法是什么?我想我可以使用if with as?,但这是否意味着其他条件我需要返回一个空单元格?那可以接受吗?

if let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellID, forIndexPath: indexPath) as? MyOffersViewCell {
      // code
} else {
      // code
}
Run Code Online (Sandbox Code Playgroud)

uitableview tableview collectionview uicollectionviewcell swift

38
推荐指数
5
解决办法
2万
查看次数