0

I realize similar question about fatal errors and unwrapping optionals have been asked, but in this case the only things I have added to my code are two outlets (I've checked their connections) and now I am experiencing errors in places that previously didn't set off errors.

My code was running well and then I added two container views and it crashes saying: "fatal error: unexpectedly found nil while unwrapping an Optional value," but I can't figure out where the optional could be. It says it has to do with a UIview called segmentBackground but that didn't set off any error before I added the container views.

Thank you for your help in advanced.

Here is my code:

class DetailMosaicViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

// Properties
var mosaic: String?
var overviewBottomBorder: UIView?
var modelsBottomBorder: UIView?
var sectionTitles = [String]() {
    didSet {
        profileTableView.reloadData()
    }
}
var mainText = [String]() {
    didSet {
        profileTableView.reloadData()
    }
}

// Outlets
@IBOutlet weak var segmentBackground: UIView!
@IBOutlet weak var profileTableView: UITableView!
@IBOutlet weak var profileModelsSegment: UISegmentedControl!
@IBOutlet weak var modelsContainerView: UIView!
@IBOutlet weak var profileContainerView: UIView!



override func viewDidLoad() {
    super.viewDidLoad()

    if let mosaic = mosaic {
        navigationItem.title = mosaic
    }
    self.navigationController?.navigationBar.tintColor = UIColor(red:0.00, green:0.87, blue:0.39, alpha:1.0)
    configureSegmentController()
    configureText()
    // Configures TableView
    profileTableView.delegate = self
    profileTableView.dataSource = self
    profileTableView.separatorColor = UIColor.black
    profileTableView.estimatedRowHeight = 140
    profileTableView.rowHeight = UITableViewAutomaticDimension
}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func configureSegmentController() {
    segmentBackground.backgroundColor = UIColor(red:0.00, green:0.87, blue:0.39, alpha:1.0)
    profileModelsSegment.center = segmentBackground.center
    profileModelsSegment.setTitle("Profile", forSegmentAt: 0)
    profileModelsSegment.setTitle("Models", forSegmentAt: 1)
    profileModelsSegment.tintColor = UIColor.white
    profileModelsSegment.selectedSegmentIndex = 0
}

func configureText() {
    // make request to firebase, store, and assign to tableView
    sectionTitles = ["Description", "Business Models", "Moats", "Competition", "Unit Economics"]
}

@IBAction func switchAction(_ sender: UISegmentedControl) {
    if sender.selectedSegmentIndex == 0 {
        UIView.animate(withDuration: 0.5, animations: {
            self.profileTableView.alpha = 1
            self.modelsContainerView.alpha = 0
        })
    } else {
        UIView.animate(withDuration: 0.5, animations: {
            self.profileTableView.alpha = 0
            self.modelsContainerView.alpha = 1
        })
    }
}

// TableView Datasource
// Customizes Section Titles
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    // Makes Background White
    let cell = tableView.dequeueReusableCell(withIdentifier: "customHeader") as UITableViewCell!
    cell?.textLabel?.text = sectionTitles[section]
    cell?.contentView.backgroundColor = UIColor.white
    return cell
}

func numberOfSections(in tableView: UITableView) -> Int {
    return sectionTitles.count
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    //configure cell
    let cell = tableView.dequeueReusableCell(withIdentifier: "profileCell", for: indexPath) as! ProfileCell

    cell.mainSectionText.text = "Box (company) Box (formerly Box.net), based in Redwood City, California, is a cloud content management and file sharing service for businesses. The company uses a freemium business model to provide cloud storage and file hosting for personal accounts and businesses."

    return cell
}

}

And this is the error message:

Error Message

My outlet connection:

connection

Ben Nalle
  • 537
  • 2
  • 8
  • 21
  • Not marking as a duplicate because I don't know enough Swift to tell, but take a look at [this](https://stackoverflow.com/a/32170457/1586231) answer and see if it answers your question. Good luck! – Max von Hippel Oct 26 '17 at 19:54
  • 1
    Thanks Max. I was looking at that question, but I still couldn't see where the optional unwrapping manifested in my code. Especially if the elements causing an error didn't cause an error prior to adding the container views. – Ben Nalle Oct 26 '17 at 19:59

1 Answers1

1

You need to initialise your segmentBackground before you can update a property. Something like segmentBackground = UIView() will suffice.

If you are connecting a UIView from a storyboard, make sure the connection is valid. In Xcode, this can be done by viewing the Connections Inspector and ensuring the correct connection has been configured:

enter image description here

Alexander MacLeod
  • 2,026
  • 3
  • 14
  • 22