nil on unwrapping ScrollView

I'm developing a feature slider by paginating ScrollView. My application crashes when I call the ViewController of the slider. I tried all possible solutions available on the internet such as clean build, reconnecting the scrollView, etc..., I'm looking for a possible fix. Thanks in advance
Code Block import UIKit
class IntroSliderViewController: UIViewController {
   
  @IBOutlet weak var slider: UIScrollView!
   
  let slide1 = ["background":"sliderBackground-1", "mainImage":"sliderMainPic-1", "heading":"....", "content":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi aenean suspendisse proin nunc risus sed. Aliquet faucibus sed commodo tellus magna cursus. Eget mauris congue at ornare."]
   
  let slide2 = ["background":"sliderBackground-2", "mainImage":"sliderMainPic-2", "heading":"....", "content":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi aenean suspendisse proin nunc risus sed. Aliquet faucibus sed commodo tellus magna cursus. Eget mauris congue at ornare."]
   
  let slide3 = ["background":"sliderBackground-3", "mainImage":"sliderMainPic-3", "heading":".....", "textContent":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi aenean suspendisse proin nunc risus sed. Aliquet faucibus sed commodo tellus magna cursus. Eget mauris congue at ornare."]
   
  var slides = [ Dictionary<String,String> ]()
   
  override func viewDidLoad() {
    super.viewDidLoad()
     
    slides = [slide1,slide2,slide3]
    slider.isPagingEnabled = true
    slider.contentSize = CGSize(width: self.view.bounds.width, height: self.view.bounds.height)
    slider.showsHorizontalScrollIndicator = false
     
    loadSlides()
  }
   
  func loadSlides() {
     
    for (index,content) in slides.enumerated(){
       
      if let slide = Bundle.main.loadNibNamed("sliderContent", owner: self, options: nil)?.first as? SliderContentView {
         
        slide.backgroundImage.image = UIImage(named: content["background"]!)
        slide.mainImage.image = UIImage(named: content["mainImage"]!)
        slide.heading.text = content["heading"]
        slide.textContent .text = content["textContent"]
         
        slider.addSubview(slide)
        slide.frame.size.width = self.view.bounds.size.width
        slide.frame.size.height = self.view.bounds.size.height
        slide.frame.origin.x = CGFloat(index) * self.view.bounds.size.width
      }
       
    }
     
  }
   
}


Answered by OOPer in 673394022
Thanks for showing the code.

This is the worst part of your code:
Code Block
IntroSliderViewController()


You are calling the undocumented initializer init() of IntroSliderViewController, in almost all cases, it would create a garbage object which causes Fatal error: Unexpectedly found nil.

When you want to instantiate a view controller that you have defined on the storyboard with subviews and/or IBOutlets/IBActions, you need to instantiate it through the storyboard.
Like this:
Code Block
let introSliderVC = self.storyboard?.instantiateViewController(withIdentifier: "IntroSliderViewController")
self.present(introSliderVC, animated: true, completion: nil)

(You need to put the right Storyboard ID IntroSliderViewController to the view controller in the Identity Inspector.)

My application crashes when I call the ViewController of the slider. 

How are you showing the view controller? Using segue? Or something else?

And please show the whole message you get and which line it happens.
The ViewController is displayed using present function.

Code Block
self.present(IntroSliderViewController(), animated: true, completion: nil)

And here is the error which occurs when I call this function:


Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value: file myPlan/IntroSliderViewController.swift, line 25
2021-05-05 16:18:30.705352+0530 myPlan[11130:802505] Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value: file myPlan/IntroSliderViewController.swift, line 25
(lldb)


Accepted Answer
Thanks for showing the code.

This is the worst part of your code:
Code Block
IntroSliderViewController()


You are calling the undocumented initializer init() of IntroSliderViewController, in almost all cases, it would create a garbage object which causes Fatal error: Unexpectedly found nil.

When you want to instantiate a view controller that you have defined on the storyboard with subviews and/or IBOutlets/IBActions, you need to instantiate it through the storyboard.
Like this:
Code Block
let introSliderVC = self.storyboard?.instantiateViewController(withIdentifier: "IntroSliderViewController")
self.present(introSliderVC, animated: true, completion: nil)

(You need to put the right Storyboard ID IntroSliderViewController to the view controller in the Identity Inspector.)
Thank you for the help. It worked.
nil on unwrapping ScrollView
 
 
Q