【发布时间】:2020-03-27 10:22:58
【问题描述】:
我在另一个视图控制器中将用户配置文件显示为搜索结果,以便在选择单元格时用户会被关注。我从 updateSearchResults 方法加载结果,它加载得很好,但是当我选择要关注该用户的行时,有时它会抛出一个错误,致命错误:索引超出范围。
我的代码:
func updateSearchResults(for searchController: UISearchController) {
self.chooseResultsTableController.reloadData()
let ref = Database.database().reference()
let queryRef : DatabaseQuery
let lastUser = self.resultsUsers.last
guard searchController.searchBar.text != "" else {
return
}
queryRef = ref.child("users").queryOrdered(byChild: "full Name").queryStarting(atValue: searchController.searchBar.text).queryEnding(atValue: searchController.searchBar.text!+"\u{f8ff}").queryLimited(toLast: 20)
SVProgressHUD.show()
var tempUsers2 = [user]()
queryRef.observeSingleEvent(of: .value, with: { snapshot in
for snap in snapshot.children {
let userSnap = snap as! DataSnapshot
let userKey = userSnap.key
let userDict = userSnap.value as! [String:AnyObject]
let aUser = user()
aUser.fullName = userDict["full Name"] as? String
let joinedSince = userDict["dateJoined"] as! Double
aUser.ImagePath = userDict["urlToImage"] as? String
aUser.about = userDict["about"] as? String
if let FollowingCount = userDict["followers"] as? [String : String] {
aUser.followers = FollowingCount.values.count
}
aUser.userId = userKey
let sensibleDate = joinedSince / 1000
let date = Date(timeIntervalSince1970: sensibleDate)
var finalString = String()
let secondsAgo = Int(Date().timeIntervalSince(date))
let minute = 60
let hour = 60 * minute
let day = 24 * hour
let week = 7 * day
if secondsAgo < minute {
// finalString = "\(secondsAgo) Just now"
finalString = "Just now"
} else if secondsAgo < hour {
finalString = "\(secondsAgo / minute) minutes ago"
} else if secondsAgo < day {
finalString = "\(secondsAgo / hour) hours ago"
} else if secondsAgo < week {
finalString = "\(secondsAgo / day) days ago"
} else {
finalString = "\(secondsAgo / week) weeks ago"
}
aUser.joinedSince = finalString
aUser.untouchedDate = joinedSince
if userKey != lastUser?.userId {
tempUsers2.insert(aUser, at: 0)
print(tempUsers2)
// self.users.removeAll()
print("\(self.resultsUsers.count) number of users")
}
}
SVProgressHUD.dismiss()
self.resultsUsers = tempUsers2
self.chooseResultsTableController.reloadData()
})
}
此外,当我使用不同的关键字搜索以使表格视图数据发生变化时,在单元格上选择时似乎可以保证错误:(
错误代码:
func checkFollowing(indexPath : IndexPath) {
let uid = Auth.auth().currentUser?.uid
let ref = Database.database().reference()
ref.child("users").child(uid!).child("following").queryOrderedByKey().observe(.value) { (snapshot) in
if let following = snapshot.value as? [String : AnyObject] {
for (_ , value) in following {
if value as? String == self.resultsUsers[indexPath.row].userId {
self.chooseResultsTableController.cellForRow(at: indexPath)?.accessoryType = .checkmark
}
}
}
}
}
【问题讨论】:
-
添加出现错误的代码
-
@Toto 添加了我收到错误的代码
-
你需要安全来打开你的选项以避免崩溃。检查这个答案stackoverflow.com/questions/25195565/…
-
@Toto 我在更正选项后尝试运行,但仍然出现该错误
-
您得到索引超出范围,请先尝试检查数组是否具有该索引,然后再访问它。检查 index
标签: ios swift firebase firebase-realtime-database