将旧的C for循环更改为新的Swift循环

时间:2017-02-09 18:51:58

标签: arrays swift loops for-loop

我有一个旧的Xcode 7.3 Swift 2代码。我需要将这两个for循环更改为新的swift 3 for循环语法。

fileprivate func collapseSubItemsAtIndex(_ index : Int) {

    var indexPaths = [IndexPath]()
    let parent = self.findParent(index)
    checkCurrentLanguage()

    if lang.isEqual(to: "en")
    {
        //For loop 1
        for (var i = index + 1; i <= index + self.engsubItems[parent].count; i += 1 ){
            indexPaths.append(IndexPath(row: i, section: 0))
        }
        self.engtableview.deleteRows(at: indexPaths, with: UITableViewRowAnimation.fade)
        self.engtotal  -= self.engsubItems[parent].count

    }
    else{
        //For loop 2
        for (var i = index + 1; i <= index + self.subItems[parent].count; i += 1 ){
            indexPaths.append(IndexPath(row: i, section: 0))
        }
        self.tableView.deleteRows(at: indexPaths, with: UITableViewRowAnimation.fade)
        self.total  -= self.subItems[parent].count

    }
}

2 个答案:

答案 0 :(得分:1)

let count = self.engsubItems[parent].count

let rowsToDelete = ((index + 1) ... (index + count))
     .map { IndexPath(row: $0, section: 0) }
self.engtableview.deleteRows(at: rowsToDelete, with: UITableViewRowAnimation.fade)
self.engtotal -= count

答案 1 :(得分:1)

for i in (index+1)...(index + self.engsubItems[parent].count) {
    ...
}
for i in (index+1)...(index + self.subItems[parent].count) {
    ...
}