Value of type '[indexPath]?' has no member 'row'

Please help, I am trying to pass data through a segue using UICollectionViewController


override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "eventDetails" {
            let vc = segue.destination as! eventDetails
            let indexPath = collectionView?.indexPathsForSelectedItems
            vc.event = events[indexPath.row]
        } else if segue.identifier == "updateevent" {
            let vc = segue.destination as! UpdateEventViewController
            let indexPath = collectionView?.indexPathsForSelectedItems
            vc.event = events[indexPath.row]
        }
    }


My poblem lies on lines 6 and 10


vc.event = events[indexPath.row]


Value of type '[indexPath]?' has no member 'row'


Not sure what I should use for item in the collectionView?!


help

Accepted Reply

As the property name `indexPathsForSelectedItems` represents, the property may return multiple `IndexPath`s, as `UICollectionView` can be configured to allow multiple selection. The type of the property is `[IndexPath]?`, so you need to choose one element from the array to use the result as a single `IndexPath`:

            if let indexPath = collectionView?.indexPathsForSelectedItems?.first {
                vc.event = events[indexPath.row]
            }

Assuming you do not allow multiple selection in your `UICollectionView`, always use the first element for your `indexPath`.

Replies

As the property name `indexPathsForSelectedItems` represents, the property may return multiple `IndexPath`s, as `UICollectionView` can be configured to allow multiple selection. The type of the property is `[IndexPath]?`, so you need to choose one element from the array to use the result as a single `IndexPath`:

            if let indexPath = collectionView?.indexPathsForSelectedItems?.first {
                vc.event = events[indexPath.row]
            }

Assuming you do not allow multiple selection in your `UICollectionView`, always use the first element for your `indexPath`.

Thank you very mucho!