配列をデータで埋める問題に直面しています。後で削除するときに、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つのユーザー、リンク1、次回は@私は2人のユーザー+リンクユーザーを取得する(ので、彼は2回)。私はそれを無限に行うことができます.100人のuserSuggestionsのように2人の既存のユーザーしかいないのです。
写真
「userSuggestions」の種類は何ですか? – Abizern
var usersSuggestion:[UserModel] = []。空の配列として始まり、doSearch()関数の中でいっぱいになります。 –