【发布时间】:2018-08-28 21:18:57
【问题描述】:
我知道这听起来很疯狂,但只是好奇如何减少 if 循环迭代以进行后续操作?我曾尝试使用guard let,但卡在某个地方。
{
if arenaEventItems == nil || arenaEventItems.count <= 0 {
return
}
if (arenaEventItems.count > 0 && (self.arenaEvents?.monthsDictObjList.count)! > 0){
if (self.tableView != nil){
if let arrVisibleRows = self.tableView.indexPathsForVisibleRows as? [IndexPath]{
if (self.tableView.indexPathsForVisibleRows!.count > 0){
let indexPath : IndexPath = self.tableView.indexPathsForVisibleRows!.first!
if let dict = self.arenaEvents?.monthsDictObjList[indexPath.row] {
if(self.arenaHeaderView != nil) && (dict.count) > 0 {
self.arenaHeaderView?.setMonthTitle(string: (dict.keys.first!))
let selectedMonthTitle = (dict.keys.first!)
for month in (self.arenaEvents?.uniqueMonthOnlyList)! {
if (selectedMonthTitle.contains(month)){
selectedMonthIndex = (self.arenaEvents?.uniqueMonthOnlyList.index(of: month)!)!
break
}
}
}
}
}
}
}
}
}
【问题讨论】:
-
您没有遵循 Swift 语言指南。将
nil与optional-binding混合多次检查。不必要的注释类型IndexPath。嵌套的if条件可以组合使用。还有更多... -
@PriyankaMistry 不要忘记为有帮助的答案投票,并用绿色复选标记接受最有帮助的答案。 :)
标签: swift performance loops if-statement reduce