我面临的问题是我填充数据的数组,然后当我想稍后删除它时,即使我调用removeAll()
该数组仍然不是空的。正确地从数组中删除数据
更清楚地看到我的代码进行更清晰的视野对这个问题
更新的新代码
@objc func textFieldDidChange() {
doSearch()
if let commentText = commentTextField.text , !commentText.isEmpty {
sendButton.setTitleColor(UIColor.blue, for: UIControlState.normal)
sendButton.isEnabled = true
return
}
sendButton.setTitleColor(UIColor.lightGray, for: UIControlState.normal)
sendButton.isEnabled = false
}
func doSearch() {
let caption = commentTextField.text
let words = caption?.components(separatedBy: CharacterSet.whitespacesAndNewlines)
self.usersSuggestion.removeAll()
for var word in words! {
self.usersSuggestion.removeAll()
if word.hasPrefix("@") {
word = word.trimmingCharacters(in: CharacterSet.punctuationCharacters)
self.isCellSelected = true
self.usersSuggestion.removeAll()
API.User.suggestUsers(withText: word, completion: { (user) in
print("closure", word)
self.usersSuggestion.append(user)
self.tableView.reloadData()
print("@", self.usersSuggestion.count)
})
self.usersSuggestion.removeAll()
tableView.reloadData()
} else {
self.isCellSelected = false
self.usersSuggestion.removeAll()
tableView.reloadData()
}
self.usersSuggestion.removeAll()
tableView.reloadData()
}
}
我在这里面临的问题是,阵列,尽管我多次调用了的removeAll ()方法,永远不会回到0.当我有2个用户时,链接一个,下一次我写@我得到2个用户+链接的用户(所以他两次)。我可以无限地做到这一点,拥有像仅有2个现有用户的100个用户建议。
照片
'userSuggestions'的类型是什么? – Abizern
var usersSuggestion:[UserModel] = []。以空数组开始,在我的doSearch()函数中填充。 –