如何减少if条件循环 - Swift

时间:2018-03-20 06:43:18

标签: swift performance loops if-statement reduce

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

3 个答案:

答案 0 :(得分:1)

你可以像这样减少它,不需要任何强制展开或嵌套:

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: ...)

答案 1 :(得分:0)

您可以将“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
            }
        }
    }

答案 2 :(得分:0)

您应该考虑重新构建代码,对于任何查看代码的人来说,您的代码都不可读并且难以理解。既然你正在使用Swift,那么使用 guard ... else 编写这样的代码非常容易,如果...让  图案。

您可以在课堂上进行的一些改进是让您的视图非零,即让它们隐式展开可选,因为您将始终将它们连接到故事板。

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

此外,你有阵列可以为零,为什么你想要它为零。您可以简单地初始化一个空数组和字典。这样你可以减少一些像这样的比较代码,

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
    }
}