0
    let saveNewItem = SaveNewItem()

    print(saveNewItem)

    if saveNewItem != nil {
        print("Contains a value!")
        _ = UIApplication.shared.keyWindow!.rootViewController
        self.present(saveNewItem, animated: true, completion: nil)
    } else {
         print("Doesn’t contain a value.")

    }

I am receiving the infamous: fatal error: unexpectedly found nil while unwrapping an Optional value

on my saveNewItem declaration, which is a call to a different viewcontroller: SaveNewItem from the main one calling this.

I'm confused why this is return nil when the print statement is stating it has a value store in the declaration.

a bypass I figured out to continue the process was to inspect (cmd+click) the saveNewItem constant to which it reproduces in the console my print statement, which gives the impression it creates an object.

I've also tried linking the viewcontroller using the storyboard as demonstated in this question: How to connect ViewController.swift to ViewController in Storyboard?

hello world
  • 306
  • 1
  • 6
  • 28
  • Are you sure it's not keyWindow that has a `nil` value? That is the only value you are force unwrapping, so I should be surprised if it wasn't that line. – Dávid Pásztor Jun 14 '17 at 12:58
  • `SaveNewItem` is a UIViewController, right? But does it appears in a Xib or a Storyboard? If YES, it may have some `IBOultet`. Then by doing `let saveNewItem = SaveNewItem()` you are not calling the one from the XIB or in the Storyboard, so all theses elements are not linked and you find yourself with an "unexpectedly found nil while unwrapping an Optional value" error. – Larme Jun 14 '17 at 12:58
  • @DávidPásztor Yes I am sure it's not keyWindow, it's the line: self.present(saveNewItem, animated: true, completion: nil) That causes the error. – hello world Jun 14 '17 at 13:02
  • @Larme SaveNewItem appears in the storyboard. How does one ensure that the story board and viewcontroller are linked. – hello world Jun 14 '17 at 13:09
  • 1
    You should use `let saveNewItem = self.storyboard?.instantiateViewController(withIdentifier: "SaveNewItem")` or whatever your Viewcontroller's identifier is. – Dávid Pásztor Jun 14 '17 at 13:23
  • @DávidPásztor would it be replacing the whole of: let saveNewStylerItem = SaveNewStylerItem() and now returning Printing description of saveNewItem: expression produced error: error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=1, address=0x69746371). – hello world Jun 14 '17 at 14:57
  • Are you sure, you set up your ViewController's identifier correctly? – Dávid Pásztor Jun 14 '17 at 15:00
  • I am sure, please ignore my usage of the words: "styler". @DávidPásztor with the self.storyboard?.instantiateViewController(withIdentifier: "SaveNewItem") declaration, the variable is nil in the inspector. – hello world Jun 14 '17 at 15:09
  • If it's nil, it really seems like your `SaveNewItem` class does not have an identifier set up. In InterfaceBuilder is the Storyboard ID of your ViewController set to _SaveNewItem_ and is _Use Storyboard ID_ checked? – Dávid Pásztor Jun 14 '17 at 15:19
  • @DávidPásztor in my storyboard, both, viewcontroller's identifier is set to SaveNewItem and use storyboard id is checked. and saveNewItem is nil. – hello world Jun 14 '17 at 15:41

2 Answers2

0

probably, your SaveNewItem() method returns nil. try this: saveNewItem? = SaveNewItem() println(saveNewItem?)

Does it return nil?

0

Found a way for it to work with no error. These properties had to be called after creating the class object saveNewItem. I overlooked that the SaveLookItems class has properties:

var itemImage     : UIImage!
var imageOrigin   : NSString!
var itemType      : NSString! 

Which require having an initial value other than nil otherwise I get the fatal error: unexpectedly found nil.

So it now works with:

let saveNewItem = SaveNewItem()

print(saveNewItem)

if saveNewItem != nil {
    print("Contains a value!")
        saveNewItem.itemImage = image
        saveNewItem.itemType = "topItem"
        saveNewItem.imageOrigin = "Camera"
    _ = UIApplication.shared.keyWindow!.rootViewController
    self.present(saveNewItem, animated: true, completion: nil)
} else {
     print("Doesn’t contain a value.")

}
hello world
  • 306
  • 1
  • 6
  • 28