4

Basically there is something wrong either with my code, Swift or the compiler/Xcode itself. I have 2 ViewControllers and the second one (destination) has 2 UIImage variables. First ViewController sets those 2 images in prepareForSegue like this:

if let destVC = segue.destinationViewController as? SecondViewController {
    destVC.firstImage = firstImage
    destVC.secondImage = secondImage
}

Every time I checked what's going on in that part of code both firstImage and secondImage are set and are never nil (they are both created before that part of code, and I even logged to check it and they were never nil), however in viewDidLoad method in SecondViewController the app crashes cause firstImage is set, but secondImage is nil (and I force unwrapped it). I've tried compiling it numerous times - every time it's nil and it crashes. After that I've added print("hello world") line to the segue code, erased it, saved the file, compiled again and app never crashed again.

So my question here is - what exactly happened - judging by code, the secondImage should never be nil (since the secondImage is never nil, nor was it nil in debugger or when I make a breakpoint), so why did the secondImage in destVC became nil?

Also I need to mention that I'm using inbuilt Git, and I'm not sure if it's maybe somehow affecting or corrupting the files since I've already had issues where Xcode sometimes takes forever to reindex the code and autocompletion stops working for around 20-30 seconds until it does that (I have an iMac Retina with an i5 so this really shouldn't be an issue).

edit: let me add the code from SecondViewController to make things clear:

...
class SecondViewController: UIViewController {
    ...
    var firstImage: UIImage?
    var secondImage: UIImage?
    ...
    override func viewDidLoad() {
        super.viewDidLoad()
        self.loadMainImage(self.firstImage!)
        self.loadSecondaryImage(self.secondImage!) // secondImage was nil all the time here so it crashed at this point cause of force unwrap
        ...
    }
    ...
}

So as you can see I'm setting everything properly, and the methods take UIImage(not optional UIImage?) and do something with those images, however secondImage was always nil. So my questions are - why is firstImage set normally, and second isn't? And why it started working normally when I added one line of code, then deleted it, and compiled everything again.

 

I'm aware that I can check if the secondImage is nil and that I can do optional unwrapping but the point here is - why did it get set to nil with no reason at all.

n1tr0
  • 269
  • 4
  • 15
  • can u show the code in second view controller view did load ? – Teja Nandamuri Mar 10 '16 at 17:51
  • how u r initialising secondImage and using it in second view controller ? – Teja Nandamuri Mar 10 '16 at 17:52
  • @TejaNandamuri added the code from SecondViewController to make things clear. Nothing funky going on, secondImage was always nil for some reason. Adding one line of code, erasing it, and then saving again and compiling and the crash was gone, and secondImage was always set as expected. – n1tr0 Mar 10 '16 at 18:44

2 Answers2

2

I was able to fix this by moving my code from viewDidLoad to viewWillAppear My class variables (set in the presenting view controller) were nil in viewDidLoad but were instantiated in the viewWillAppear The strange thing is that they worked in the viewDidLoad for a while. This seems to indicate a race condition, though I'm not sure that makes any sense.

Jbryson
  • 2,875
  • 1
  • 31
  • 53
1

You can pass image to another image property of the second VC and then set the "passed" image in the viewDidLoad of the SecondVC. BUT you cannot pass or set the image directly to that of the imageView.image of the SecondVC, as secondVC.imageView is not yet allocated in memory.

What you need to do is create an UIImage Property in the .h of the secondVC and pass the image to that Image Property. Set the image in the viewDidLoad

Saheb Roy
  • 5,899
  • 3
  • 23
  • 35
  • If you carefully look at my question you will see that it's Swift code so there is no need for an .h file cause everything is defined in 1 .swift file. And yes both UIImages are declared in SecondViewController - if they weren't Xcode wouldn't even let me compile that code. – n1tr0 Mar 10 '16 at 18:32
  • @saheb-roy you ended my endless search for a fix. Thank you! – Ali Jul 12 '19 at 11:31