【发布时间】:2017-09-02 12:47:25
【问题描述】:
我面临的问题是,我用数据填充了一个数组,当我想稍后删除它时,即使我调用 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的类型是什么? -
var usersSuggestion:[UserModel] = []。从一个空数组开始,在我的 doSearch() 函数中填充。
标签: arrays swift firebase-realtime-database