IOS 开发的新手,单击评论单元格线程 1 时崩溃:信号 SIGABRT 并由于未捕获的异常而终止应用程序
New to IOS development, getting crash when clicking on comment cell Thread 1: signal SIGABRT and terminating app due to uncaught exception
应用程序构建成功,但在 运行 应用程序之后,在我点击评论单元格后,应用程序崩溃,导致错误:
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSSingleObjectArrayI objectAtIndex:]: index 7 beyond bounds [0 .. 0]' and Thread 1: signal SIGABRT.
在下图中,每当我点击带有随机评论的任何 Alec1 时,应用程序都会崩溃。
https://i.ibb.co/Sn5gFZ3/Screen-Shot-2019-04-22-at-12-49-19-AM.png
var posts = [PFObject]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
// Do any additional setup after loading the view.
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let query = PFQuery(className: "Posts")
query.includeKeys(["author", "comments", "comments.author"])
query.limit = 20
query.findObjectsInBackground{ (posts, error) in
if posts != nil{
self.posts = posts!
self.tableView.reloadData()
}
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let post = posts[section]
let comments = (post["comments"] as? [PFObject]) ?? []
return comments.count + 1
}
func numberOfSections(in tableView: UITableView) -> Int {
return posts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let post = posts[indexPath.section]
let comments = (post["comments"] as? [PFObject]) ?? []
if indexPath.row == 0{
let cell = tableView.dequeueReusableCell(withIdentifier: "PostCell") as! PostCell
let user = post["author"] as! PFUser
cell.usernameLabel.text = user.username
cell.captionLabel.text = post["caption"] as! String
let imageFile = post["image"] as! PFFileObject
let urlString = imageFile.url
let url = URL(string: urlString!)!
cell.photoView.af_setImage(withURL: url)
return cell
}else{
let cell = tableView.dequeueReusableCell(withIdentifier: "CommentCell") as! CommentCell
let comment = comments[indexPath.row - 1]
cell.commentLabel.text = comment["text"] as? String
let user = comment ["author"] as! PFUser
cell.nameLabel.text = user.username
return cell
}
}
//creates new columns
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let post = posts[indexPath.row]
let comment = PFObject(className: "Comments")
comment["text"] = "random comment"
comment["post"] = "post"
comment["author"] = PFUser.current()
post.add(comment, forKey: "comments")
post.saveInBackground{(success, error) in
if success{
print("comment saved!")
}else{
print("error saving comments")
}
}
}
预期结果是点击评论单元格的某些部分后,应用程序不应崩溃
在didSelectRowAt
中使用section
获取post
然后选择评论,
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let post = posts[indexPath.section]
let comments = (post["comments"] as? [PFObject]) ?? []
let comment = comments[indexPath.row]
comment["text"] = "random comment"
comment["post"] = "post"
comment["author"] = PFUser.current()
post.add(comment, forKey: "comments")
post.saveInBackground{(success, error) in
if success{
print("comment saved!")
}else{
print("error saving comments")
}
}
}
您的文章数组为空,添加检查
guard !posts.isEmpty else { return }
应用程序构建成功,但在 运行 应用程序之后,在我点击评论单元格后,应用程序崩溃,导致错误:
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSSingleObjectArrayI objectAtIndex:]: index 7 beyond bounds [0 .. 0]' and Thread 1: signal SIGABRT.
在下图中,每当我点击带有随机评论的任何 Alec1 时,应用程序都会崩溃。
https://i.ibb.co/Sn5gFZ3/Screen-Shot-2019-04-22-at-12-49-19-AM.png
var posts = [PFObject]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
// Do any additional setup after loading the view.
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let query = PFQuery(className: "Posts")
query.includeKeys(["author", "comments", "comments.author"])
query.limit = 20
query.findObjectsInBackground{ (posts, error) in
if posts != nil{
self.posts = posts!
self.tableView.reloadData()
}
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let post = posts[section]
let comments = (post["comments"] as? [PFObject]) ?? []
return comments.count + 1
}
func numberOfSections(in tableView: UITableView) -> Int {
return posts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let post = posts[indexPath.section]
let comments = (post["comments"] as? [PFObject]) ?? []
if indexPath.row == 0{
let cell = tableView.dequeueReusableCell(withIdentifier: "PostCell") as! PostCell
let user = post["author"] as! PFUser
cell.usernameLabel.text = user.username
cell.captionLabel.text = post["caption"] as! String
let imageFile = post["image"] as! PFFileObject
let urlString = imageFile.url
let url = URL(string: urlString!)!
cell.photoView.af_setImage(withURL: url)
return cell
}else{
let cell = tableView.dequeueReusableCell(withIdentifier: "CommentCell") as! CommentCell
let comment = comments[indexPath.row - 1]
cell.commentLabel.text = comment["text"] as? String
let user = comment ["author"] as! PFUser
cell.nameLabel.text = user.username
return cell
}
}
//creates new columns
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let post = posts[indexPath.row]
let comment = PFObject(className: "Comments")
comment["text"] = "random comment"
comment["post"] = "post"
comment["author"] = PFUser.current()
post.add(comment, forKey: "comments")
post.saveInBackground{(success, error) in
if success{
print("comment saved!")
}else{
print("error saving comments")
}
}
}
预期结果是点击评论单元格的某些部分后,应用程序不应崩溃
在didSelectRowAt
中使用section
获取post
然后选择评论,
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let post = posts[indexPath.section]
let comments = (post["comments"] as? [PFObject]) ?? []
let comment = comments[indexPath.row]
comment["text"] = "random comment"
comment["post"] = "post"
comment["author"] = PFUser.current()
post.add(comment, forKey: "comments")
post.saveInBackground{(success, error) in
if success{
print("comment saved!")
}else{
print("error saving comments")
}
}
}
您的文章数组为空,添加检查
guard !posts.isEmpty else { return }