用代码:
let indexPathToDelList:[(Int, Int)] = [(Int, Int)]() for (sectionIdx, eachSection) in contactSectionItemList.enumerate() { if !(isFixedTopSection(eachSection)) { for (idxInSection, eachContactItem) in eachSection.contactItemList.enumerate() { if eachContactItem.type == ContactType.TeamColleague { contactSectionItemList[sectionIdx].contactItemList.removeAtIndex(idxInSection) print("contactSectionItemList[sectionIdx]=\(contactSectionItemList[sectionIdx])") } } } } |
执行时出错:
需要去研究出一种办法:
如何实现在循环中,删除对应的元素
swift delete during loop
swift delete during enumeration
ios – Removing from array during enumeration in Swift? – Stack Overflow
Selectively remove and delete objects from a NSMutableArray in Swift – Stack Overflow
objective c – Best way to remove from NSMutableArray while iterating? – Stack Overflow
func removeTeamColleagueContactItems() { for var sectionIdx = contactSectionItemList.count – 1; sectionIdx >= 0 ; –sectionIdx { if !(isFixedTopSection(contactSectionItemList[sectionIdx])) { for var contactIdxInSection = contactSectionItemList[sectionIdx].contactItemList.count – 1; contactIdxInSection >= 0 ; –contactIdxInSection { if contactSectionItemList[sectionIdx].contactItemList[contactIdxInSection].type == ContactType.TeamColleague { //print("deleted team colleague [\(sectionIdx) – \(contactIdxInSection)]=\(contactSectionItemList[sectionIdx].contactItemList[contactIdxInSection])") contactSectionItemList[sectionIdx].contactItemList.removeAtIndex(contactIdxInSection) } } //remove empty section if contactSectionItemList[sectionIdx].contactItemList.count == 0 { //print("delete empty section \(sectionIdx) =\(contactSectionItemList[sectionIdx].indexTitleStr)") contactSectionItemList.removeAtIndex(sectionIdx) } } } } |