0

我有以下for循环:

func test(_ fourHundredYears: [CountPatternPair]) {

    // Iterate over `CountPatternPairs` containing hundred-year `CountPatternPairs`
    for hundredYearCPP in fourHundredYears {

        // Do the below as many times as indicated by the count in the `CountPatternPair`
        for _ in 1 ... hundredYearCPP.0 {

            // Iterate over `CountPatternPairs` containing four-year `CountPatternPairs`
            for fourYearCPP in (hundredYearCPP.1 as! [CountPatternPair]) {

                // Do the below as many times as indicated by the count in the `CountPatternPair`
                for _ in 1 ... fourYearCPP.0 {

                    // Iterate over `CountPatternPairs` containing year `CountPatternPairs
                    for yearCPP in (fourYearCPP.1 as! [CountPatternPair]) {

                        // Do the below as many times as indicated by the count in the `CountPatternPair`
                        for _ in 1 ... yearCPP.0 {

                            // Iterate over each month in the array element of the `CountPatternPair`
                            for month in (yearCPP.1 as! [[DayTotalPair]]) {

                                // Iterate over each `DayTotalPair` in each month
                                for dtp in month {

                                    oeod.append(dtp.1, over: dtp.0)

                                    i += dtp.0

                                    if minLimit.equals(oeod.at(oeod.count - 1)) { return }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

我写了这个怪物来测试一些逻辑,目的是当逻辑正常工作时,我会将它重构为递归函数以使其更清晰。我做到了,但递归版本不起作用。想我错过了什么,但最终我把它搁置了,因为我一生都看不到它是什么。仍在测试中,我决定将一些循环逻辑提取到它们自己的函数中,以使其更清晰。不添加任何新逻辑。从我看来,不改变任何逻辑的执行顺序。

但令我沮丧的是,重构为单个功能的相同功能也不起作用。直接复制粘贴,还是不行。尝试使用该extract to功能Xcode以防是我的复制和粘贴破坏了它,但也没有用。

有人可以看一下上面代码的以下提取版本,看看是否有任何事情会导致它执行与上面不同的逻辑?

func iterateFourHundredYears(_ fourHundredYears: [CountPatternPair]) {

    // Iterate over `CountPatternPairs` containing hundred-year `CountPatternPairs`
    for hundredYearCPP in fourHundredYears {

        iterateHundredYearCPP(hundredYearCPP)
    }
}

func iterateHundredYearCPP(_ hundredYearCPP: CountPatternPair) {

    // Do the below as many times as indicated by the count in the `CountPatternPair`
    for _ in 1 ... hundredYearCPP.0 {

        // Iterate over `CountPatternPairs` containing four-year `CountPatternPairs`
        for fourYearCPP in (hundredYearCPP.1 as! [CountPatternPair]) {

            iterateFourYearCPP(fourYearCPP)
        }
    }
}

func iterateFourYearCPP(_ fourYearCPP: CountPatternPair) {

    // Do the below as many times as indicated by the count in the `CountPatternPair`
    for _ in 1 ... fourYearCPP.0 {

        // Iterate over `CountPatternPairs` containing year `CountPatternPairs
        for yearCPP in (fourYearCPP.1 as! [CountPatternPair]) {

            iterateYearCPP(yearCPP)
        }
    }
}

func iterateYearCPP(_ yearCPP: CountPatternPair) {

    // Do the below as many times as indicated by the count in the `CountPatternPair`
    for _ in 1 ... yearCPP.0 {

        // Iterate over each month in the array element of the `CountPatternPair`
        for month in (yearCPP.1 as! [[DayTotalPair]]) {

            // Iterate over each `DayTotalPair` in each month
            for dtp in month {

                oeod.append(dtp.1, over: dtp.0)

                i += dtp.0

                if minLimit.equals(oeod.at(oeod.count - 1)) { return }
            }
        }
    }
}
4

0 回答 0