我收到以下错误“线程1致命错误索引超出范围”,该错误出现在我的feedViewController和AppDelegate中
这可能是什么原因?我对编码非常陌生,主要是按照教程学习,但是尚未涵盖此错误。
@IBOutlet weak var tableView: UITableView!
var userEmailArray = [String]()
var userCommentArray = [String]()
var likeArray = [Int]()
var userImageArray = [String]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
// Do any additional setup after loading the view.
getDataFromFirestore()
}
func getDataFromFirestore() {
let fireStoreDatabase = Firestore.firestore()
/* let settings = fireStoreDatabase.settings
settings.areTimestampsInSnapshotsEnabled = true
fireStoreDatabase.settings = settings
*/
fireStoreDatabase.collection("Posts").order(by: "date", descending: true).addSnapshotListener { (snapshot, error) in
if error != nil {
print(error?.localizedDescription)
} else {
if snapshot?.isEmpty != true && snapshot != nil {
self.userImageArray.removeAll(keepingCapacity: false)
self.userEmailArray.removeAll(keepingCapacity: false)
self.userCommentArray.removeAll(keepingCapacity: false)
self.likeArray.removeAll(keepingCapacity: false)
for document in snapshot!.documents {
let documentID = document.documentID
if let postedBy = document.get("postedBy") as? String {
self.userEmailArray.append(postedBy)
}
if let postComment = document.get("postComment") as? String {
self.userCommentArray.append(postComment)
}
if let likes = document.get("likes") as? Int {
self.likeArray.append(likes)
}
if let imageUrl = document.get("imageUrl") as? String {
self.userImageArray.append(imageUrl)
}
}
self.tableView.reloadData()
}
}
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return userEmailArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! FeedCell
cell.userEmailLabel.text = userEmailArray[indexPath.row]
cell.likeLabel.text = String(likeArray[indexPath.row])
cell.commentLabel.text = userCommentArray[indexPath.row]
cell.userImageView.sd_setImage(with: URL(string: self.userImageArray[indexPath.row]))
return cell
}
}
这是错误所在的行:
cell.userImageView.sd_setImage(with: URL(string: self.userImageArray[indexPath.row]))