how do i solve NSRangeException into swift 4?

476 views Asked by At

I am working on app where I using swift 4. But when I want to go from first viewcontroller to second viewcontroller I got this error

* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayI objectAtIndex:]: index 18446744073709551615 beyond bounds [0 .. 0]'

I tried to go from first viewcontroller to second viewcontroller on button click using like bellow

let vc = storyBoard.instantiateViewControllerWithIdentifier("nextView") as NextViewController
self.presentViewController(vc, animated:true, completion:nil)

But when I clicked button my app get crashed and get above nsrangeexception error

So how do I solve this error any other solution to go from first vc to second vc?

2

There are 2 answers

0
Ivan Smetanin On

First of all check that your NextViewController has Storyboard ID in the storyboard:

enter image description here

And check that you're loading storyboard correctly:

let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
0
Rashed On

If you want to navigate through Controller on StoryBoard with Identifier "nextView", then do this(this will applicable when you are trying to show one storyboard's ViewController to another storyboard ViewController):

   let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
   let vc = storyBoard.instantiateViewController(withIdentifier: "nextView") as! NextViewController
   self.present(vc, animated:true, completion:nil)

OR If you are in same storyBoard then you can simply do this - Take a button in your firstVC and ctrl+ drag a segue upto your secondVC. Then click on the segue. (the round thing on between two view controller). -

enter image description here

And set a identifier for that (here about is the identifier, you can give any name for this)-

enter image description here

Here is the code -

@IBAction func UserDetailVC(sender: AnyObject) {
        self.performSegue(withIdentifier: "segueIdentifierName(about)", sender: sender)
    }