如何减少 if 条件循环 - Swift



我知道这听起来很疯狂,但只是好奇如何减少以下的 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
                                }
                            }
                        }
                    }
                }
            }
         }
       }
    }

你可以这样减少它,没有任何强制解包或嵌套:

guard let arenaEventItems = arenaEventItems,
    !arenaEventItems.isEmpty,
    let arenaEvents = self.arenaEvents,
    !arenaEvents.monthsDictObjList.isEmpty,
    let arenaHeaderView = self.arenaHeaderView,
    let indexPath = self.tableView?.indexPathsForVisibleRows?.first,
    let selectedMonthTitle = arenaEvents.monthsDictObjList[indexPath.row].keys.first
    else {
        return
}
arenaHeaderView.setMonthTitle(string: selectedMonthTitle)
if let monthIndex = arenaEvents.uniqueMonthOnlyList.index(where: { selectedMonthTitle.contains($0) }) {
    selectedMonthIndex = monthIndex
}
  • if ... return替换为 guard !... else return 以避免嵌套
  • 最佳做法是将.count > 0替换为!...isEmpty
  • self.something?的多次访问替换为if let something = self.something以避免线程问题
  • 你解开for ... in ... { if (...) { ... } }.index(where: ...)

你可以组合 "if" 中的所有条件,得到这样的结果:

if let eventItems = arenaEventItems,
   eventItems.count > 0,
   let events = self.arenaEvents,
   !events.monthsDictObjList.isEmpty,
   let tableView = self.tableView,
   let arrVisibleRows = self.tableView.indexPathsForVisibleRows as? [IndexPath],
   !arrVisibleRows.isEmpty,
   let indexPath : IndexPath = arrVisibleRows.first,
   let dict = events.monthsDictObjList[indexPath.row],
   let headerView = self.arenaHeaderView,
   !dict.isEmpty {
        headerView.setMonthTitle(string: (dict.keys.first!))
        let selectedMonthTitle = (dict.keys.first!)
        for month in events.uniqueMonthOnlyList! {
            if (selectedMonthTitle.contains(month)){
                selectedMonthIndex = (events.uniqueMonthOnlyList.index(of: month)!)!
                break
            }
        }
    }

你应该考虑重组你的代码,你的代码对于任何看它的人来说都是不可读和不可理解的。由于您使用的是 Swift,因此使用 guard 编写此类代码真的很容易......否则如果...让 模式。

您可以在类上做的一些改进是让您的视图非零,即使它们隐式解包可选,因为您将始终将它们连接到故事板。

 @IBOutlet var tableView: UITableView!
 @IBOutlet var arenaHeaderView: ArenaHeaderView!

另外,你有可以变为 nil 的数组,为什么您希望它为 nil。您可以简单地初始化一个空数组和字典。这样你就可以减少更多的比较代码,就像这样,

arenaEventItems: [String: String] = [:]

通过这些更改和一些重构,您可以将代码重写为类似这样的东西,

guard !arenaEventItems.isEmpty,
    let arenaEvents = arenaEvents,
    let indexPath = tableView.indexPathsForVisibleRows?.first,
    let dict = arenaEvents.monthsDictObjList[indexPath.row],
    let selectedMonthTitle = dict.keys.first
    else {
        return
}
arenaHeaderView.setMonthTitle(string: selectedMonthTitle)
for month in arenaEvents.uniqueMonthOnlyList where selectedMonthTitle.contains(month) {
    if let selectedIndex = arenaEvents.uniqueMonthOnlyList.index(of: month) {
        selectedMonthIndex = selectedIndex
        break
    }
}

最新更新