0

I want to move from one view controller to another and send userId:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    self.performSegueWithIdentifier("chosenPerson", sender: self)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "chosenPerson" {

        let chosenPerson = segue.destinationViewController as? chosenPersonViewController
        let indexPaths = self.collectionView!.indexPathsForSelectedItems()!
        let indexPath = indexPaths[0] as NSIndexPath

        chosenPerson!.userID = self.usersArray[indexPath.row].userId

    }

by clicking I get: "fatal error: unexpectedly found nil while unwrapping an Optional value" what I do wrong?

alex1511
  • 435
  • 7
  • 19
Denis Windover
  • 445
  • 2
  • 6
  • 20
  • Which line does the runtime break on? – Jacob King Oct 03 '16 at 11:32
  • Just a few pointers to get you started. Try stepping through the lines in your `if sentence` line by line, to get an idea about whats going on. Potential candidates for failure are `self.collectionView!.indexPathsForSelectedItems()!` and `chosenPerson!.userID = self.usersArray[indexPath.row].userId` so try to verity that `indexPathsForSelectedItems()` returns something useful and that your usersArray on position `indexPath.row` actually contains something. – pbodsk Oct 03 '16 at 11:35
  • chosenPerson!.userID = self.usersArray[indexPath.row].userId - fatal error – Denis Windover Oct 03 '16 at 11:38
  • Ah...there you go then :) It seems your `usersArray` does not contain a value that matches `indexPath.row`. Can you verify that this is indeed the problem? – pbodsk Oct 03 '16 at 11:41
  • I've tried to check it by: func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { print(usersArray[indexPath.row].userId) self.performSegueWithIdentifier("chosenPerson", sender: self) } and its not nil – Denis Windover Oct 03 '16 at 11:45
  • even in prepare for segue its not nil and " print(usersArray[indexPath.row].userId)" give me Optional("QRlQzSGNtRWgZtt87KXlW2sM15d2") – Denis Windover Oct 03 '16 at 11:51
  • 1
    That means your viewController object `chosenPerson`is nil, check in storyboard the `chosenPerson` segue it must be connected with some other controller instead of `chosenPersonViewController`. – Nirav D Oct 03 '16 at 11:57
  • why don't you declare a var like `myindexPath` and you pass the index path of the selected cell in `didSelectItemAtIndexPath`. – Mat Oct 03 '16 at 12:02
  • Possible duplicate: http://stackoverflow.com/questions/24040692/prepare-for-segue-in-swift?rq=1 – alex1511 Oct 03 '16 at 23:27

3 Answers3

3

If you have given segue in StoryBoard DON'T Call this self.performSegueWithIdentifier("chosenPerson", sender: self) method in didSelectItem

If you have given segue in storyboard override func prepareForSegue - this method calls first after that didSelectItem calls

Please refer storyBoard once (below Image for Sample)

I think problem is in self.usersArray[indexPath.row].userId May this returns nil

Swift2:

self.performSegueWithIdentifier("chosenPerson", sender: self)

Swift3:

self.performSegue(withIdentifier: "chosenPerson", sender: self)

Swift2:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

if segue.identifier == "chosenPerson" {

    let chosenPerson = segue.destinationViewController as? chosenPersonViewController
    let indexPaths = self.collectionView!.indexPathsForSelectedItems()!
    let indexPath = indexPaths[0] as NSIndexPath

    chosenPerson!.userID = self.usersArray[indexPath.row].userId //May it found nil please re - check array values

}

Swift3:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.identifier == "chosenPerson" {

        let chosenPerson = segue.destination as! chosenPersonViewController

        if let indexPath = collectionView.indexPathForSelectedItem {

        chosenPerson!.userID = self.usersArray[indexPath.row].userId //May it found nil please re - check array values

        }




    }
}

enter image description here

Sanju
  • 1,148
  • 11
  • 26
1

When performing the segue, pass the indexPath as the sender and try using this switch statement. If you see "unknown segue" printed out when you select a cell, the destination controller is not of type chosenPersonViewController.

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

    performSegueWithIdentifier("chosenPerson", sender: indexPath)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    switch (segue.destinationViewController, sender) {
    case (let controller as chosenPersonViewController, let indexPath as NSIndexPath):
        controller.userID = usersArray[indexPath.row].userId
    default:
        print("unknown segue")
        break
    }
}
Callam
  • 11,409
  • 2
  • 34
  • 32
0

One possible issue could be that in your storyboard you have not set the identifier for your segue to be "chosenPerson". To fix this first make sure you have a segue between your first and second view controllers, which can be created by control dragging one from one view controller to another.

Then make sure the segue has an identifier, specifically: "chosenPerson". To do this: click on the segue between the first two view controllers, then navigate to the attributes tab, and then set the identifier box to be "chosenPerson". Save the storyboard with your changes and now you should be able to call prepareForSegue with that identifier and not experience a fatal error.

alex1511
  • 435
  • 7
  • 19