SWIFT:线程1:致命错误:索引超出范围
在过去的4个小时里,我一直在尝试如何修复这个错误。它构建得非常完美,运行良好。但是当我在feed上滑动以重新加载时,它崩溃了 它应该做的是,你有你的家庭饲料与所有最新的帖子,并重新加载页面,你刷了。向上滑动时,它会在顶部显示一个旋转的轮子,它会从提要中删除所有图像,并将新的和更新的图像带回来。(这是一个非常糟糕的解释) 感谢您的帮助。我提供了我的整个课程只是为了确保我不会错过任何我应该发送的代码 这是我的FeedController.swift课程SWIFT:线程1:致命错误:索引超出范围,swift,Swift,在过去的4个小时里,我一直在尝试如何修复这个错误。它构建得非常完美,运行良好。但是当我在feed上滑动以重新加载时,它崩溃了 它应该做的是,你有你的家庭饲料与所有最新的帖子,并重新加载页面,你刷了。向上滑动时,它会在顶部显示一个旋转的轮子,它会从提要中删除所有图像,并将新的和更新的图像带回来。(这是一个非常糟糕的解释) 感谢您的帮助。我提供了我的整个课程只是为了确保我不会错过任何我应该发送的代码 这是我的FeedController.swift课程 import Firebase privat
import Firebase
private let reuseIdentifier = "Cell"
class FeedController: UICollectionViewController {
// MARK: - Lifecycle
private var posts = [Post]()
var post: Post?
override func viewDidLoad() {
super.viewDidLoad()
configureUI()
fetchPosts()
}
//MARK: - Actions
@objc func handleRefresh() {
posts.removeAll()
fetchPosts()
}
@objc func handleLogout() {
do {
try Auth.auth().signOut()
let controller = LoginController()
controller.delegate = self.tabBarController as? MainTabController
let nav = UINavigationController(rootViewController: controller)
nav.modalPresentationStyle = .fullScreen
self.present(nav, animated: true, completion: nil)
} catch {
print("DEBUG: Failed to sign out")
}
}
// MARK: - API
func fetchPosts() {
guard post == nil else { return }
PostService.fetchPosts { posts in
self.posts = posts
self.collectionView.refreshControl?.endRefreshing()
self.collectionView.reloadData()
}
}
// MARK: - Helpers
func configureUI() {
collectionView.backgroundColor = .white
collectionView.register(FeedCell.self, forCellWithReuseIdentifier: reuseIdentifier)
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Logout",
style: .plain,
target: self,
action: #selector(handleLogout))
navigationItem.title = "Feed"
let refresher = UIRefreshControl()
refresher.addTarget(self, action: #selector(handleRefresh), for: .valueChanged)
collectionView.refreshControl = refresher
}
}
// MARK: - UIcollectionViewDataSource
extension FeedController {
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return post == nil ? posts.count : 1
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! FeedCell
if let post = post {
cell.viewModel = PostViewModel(post: post)
} else {
cell.viewModel = PostViewModel(post: posts[indexPath.row])
}
return cell
}
}
// MARK: - UICollectionViewDelegateFlowLayout
extension FeedController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = view.frame.width
var height = width + 8 + 40 + 8
height += 50
height += 60
return CGSize(width: width, height: height)
}
}
这是给出错误的行(在我刷新之后)
cell.viewModel=PostViewModel(post:posts[indexath.row])
Ans:当您没有获得任何数据时,显示虚拟数据或返回0个单元格
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if post.count == nil { return 0 } else { return post.count
}
问题:如果数据为nil,则返回1,这意味着编译器返回一个单元格。当该单元格的每个元素都绑定到数组中的特定值时。由于post数组为nil,因此它返回范围异常的索引。重写func collectionView(\u collectionView:UICollectionView,cellForItemAt indexPath:indexPath)->UICollectionViewCell{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! FeedCell
if let post = post {
cell.viewModel = PostViewModel(post: post)
} else {
if posts.count != 0{
cell.viewModel = PostViewModel(post: posts[indexPath.row])
}
}
return cell
}在这一行
返回后==nil?posts.count:1
为什么返回1而不是0?post的属性是什么?它从来没有被设置过,而且似乎没有任何意义?什么是PostViewModel
?为什么要使用post
,因为您没有在post
中的任何位置设置任何值?尝试将posts.removeAll()移动到PostService.fetchPosts中{posts in
所以只有当你得到一些东西时才清空数组。为了获得更多信息,我在处理firebase内容的另一个类中使用post。您好,我已经尝试过了,但没有解决,我仍然遇到崩溃。这里的错误是cell.viewModel=PostViewModel(post:posts[indexPath.row])对于线程1:致命错误:索引超出range@ZachLudwick您已经初始化了post变量“var post:post?”。但是,您没有在其中获取任何值。您还没有编写任何代码来获取“post”中的值。请先实现它。将回调值获取到当前类中的“post”数组中。