0

I'm new to this so bear with me please. I have a Master-Detail app and am having issues when running the app in Portrait iPad. Master shows a table view, that when cell is clicked on, an image shows up on the Detail page. It's crashing on the imageView1.image = MasterViewController.MyVariables.flowerImage! Note that this works in iPhone. I think what's happening is because the flowerImage has not yet loaded so there is no value. By the way, the error is: fatal error: unexpectedly found nil while unwrapping an Optional value.

override func viewDidLoad() {
    super.viewDidLoad()

     imageView1.image = MasterViewController.MyVariables.flowerImage!
    // Do any additional setup after loading the view, typically from a nib.
    configureView()
}

Code for SplitViewController:

import UIKit

class SplitViewController: UISplitViewController, UISplitViewControllerDelegate {

override func viewDidLoad() {
    self.delegate = self
    self.preferredDisplayMode = .allVisible
}

func splitViewController(
    _ splitViewController: UISplitViewController,
    collapseSecondary secondaryViewController: UIViewController,
    onto primaryViewController: UIViewController) -> Bool {
    // Return true to prevent UIKit from applying its default behavior
    return true
}

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

}

Toto
  • 89,455
  • 62
  • 89
  • 125
ms_abc
  • 31
  • 11

1 Answers1

0

When you're not sure that the object can't be nil, then don't use force unwrapping.

You can use several options: 1. "guard let" statement:

guard let image = MasterViewController.MyVariables.flowerImage else{
    //add some notification about error here
    return
}
imageView1.image = image
  1. "if let" statement

    if let image = MasterViewController.MyVariables.flowerImage { imageView1.image = image }else { //add some notification about error here }

Woof
  • 1,207
  • 1
  • 11
  • 21