Search code examples
swiftrealm

Index 0 is out of bounds (must be less than 0) Error when passing realm object through segue


I have a collectionViewController and detailViewController. I need to pass object from CVController to detail controller via segue. Here is the object of a class im passing:

class FilmCategory: Object {
    @objc dynamic var id = 0
    @objc dynamic var name = ""
    var films = List<Film>() 

override static func primaryKey() -> String? {
        return "id"
    }

and here is Film class:

class Film: Object {
    @objc dynamic var id = 0
    @objc dynamic var name = ""
    var actors = List<String>()
    @objc dynamic var imageURL = ""
    @objc dynamic var duration = 0


    override static func primaryKey() -> String? {
        return "id"
    }

PS: Im initialising objects from JSON, everything is working fine, in my collectionViewController im retrieving FilmCategories like this:

class CategoriesViewController: UICollectionViewController {
    let realm = try! Realm()
    let serializer = JSONSerializer()
    var filmCategories: Results<FilmCategory>?


override func viewDidLoad() {
        super.viewDidLoad()
        registerCellWithNib()
        serializer.serialize(input: URLs.jsonCategoriesURL)
        filmCategories = realm.objects(FilmCategory.self).sorted(byKeyPath: "id",ascending: true)
    }

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if let categories = filmCategories {
            return categories.count
        } else {
            return 0
        }
    }

I didn't include cell for row at method, because everything is loading properly in collectionView, but when im clicking on the cell and performing segue, Im receiving index out of bounds error:

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        self.performSegue(withIdentifier: SegueIdentifiers.selectCategory, sender: indexPath)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == SegueIdentifiers.selectCategory , let CategoryIndex = sender as? IndexPath, let filmCategories = filmCategories {
            if let controller = segue.destination as? FilmsViewController {
                controller.currentCategoryIndex = CategoryIndex.row
                controller.category = filmCategories[CategoryIndex.row]
            }
        }
    }
}

Here is brief description of my detail view controller class:

class FilmsViewController: UIViewController {
    let realm = try! Realm()
    var currentCategoryIndex = Int()
    @objc dynamic var category: FilmCategory?

Every time im performing segue, im receiving error, probably its incorrect way to pass category via segue. Should I just send its index at query it after?


Solution

  • The sender in your prepare(for seque: sender:) is going to be an instance of UICollectionViewCell (not an index path). You probably want to get a reference to the collection view itself. Its indexPathsForSelectedItems property will give you the section and row data you're looking for.