Crashes on Long Press

let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(gesture:)))
       
            calendarView.addGestureRecognizer(longPressGesture)
       
       
           }
           @objc func handleLongPress(gesture: UILongPressGestureRecognizer) {
                guard gesture.state == .began else {return}
                let position = gesture.location(in: collectionView)
                if let indexPath = collectionView.indexPathForItem(at: position) {
                    guard let modalCalVC = self.storyboard?.instantiateViewController(identifier: "ModalCalViewController") as? ModalCalViewController else {
                        print("ModalCalViewController cannot be instantiated")
                        return
                    }
                    let value = true //Get the value you want to pass from `indexPath`
                    modalCalVC.passedValue = value
                    present(modalCalVC, animated: true, completion: nil)
                } else {
                    print("Long press not on a cell")
                }
          
           }

I dont understand why it crashes I get Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value: file.swift, Line 10

What is line 10 of file.swift ?


I don't think it is line 10 of the code you posted.

Otherwise, have you checked that collectionView is properly connected to its IBOutlet ?

And that its delegate and datasource are properly set ?


Test by adding after line 8


print(#function, collection)


So please, show the relevant part of code of file.swift.

Crashes on Long Press
 
 
Q