2017-02-09 106 views
0

我有舊的Xcode 7.3 Swift 2代碼。我需要將這兩個for循環更改爲新的swift 3 for循環語法。將舊的C for循環更改爲新的Swift循環

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 

    } 
} 
+0

爲什麼就不能'郎== 「恩」'? – Sulthan

+0

@Sulthan我剛剛收到這段代碼,他們需要將它全部更新到swift 3.有很多編碼錯誤,我仍然沒有修復或改進。 –

回答

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
for i in (index+1)...(index + self.engsubItems[parent].count) { 
    ... 
} 
for i in (index+1)...(index + self.subItems[parent].count) { 
    ... 
}