n1tr0
n1tr0

Reputation: 269

Destination's View Controller variable becoming nil after being set

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.

Upvotes: 4

Views: 1487

Answers (2)

Jbryson
Jbryson

Reputation: 2905

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.

Upvotes: 2

Saheb Roy
Saheb Roy

Reputation: 5957

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

Upvotes: 1

Related Questions