Why does the value of my variable revert in Swift?

JackElia

I got my code working, but I don't know why it works.

Having this prepareForSegue Function doesn't work, and when my variable is called in the viewController that the prepareForSegue goes to, it returns nil.

The original VC:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == K.latinSegue {
        if latinTextField.text != nil && latinTextField.text != "" {
            guard let latinText = latinTextField.text else { fatalError("latinText broken")}
            let destinationVC = LatinViewController()
            destinationVC.latinText = latinText
            
        }
    }
}

The destinationVC:

var latinText: String?

@IBOutlet weak var textLabel: UILabel!

override func viewDidLoad() {
    let latinTranslator = LatinTranslator()
    let latinDefinition = latinTranslator.getHTMLAndDefinition(latinText: **latinText!**)
    textLabel.text = latinDefinition
}

latinText! Doesn't have a value when it is force unwrapped here, even though I set its value in the prepare for segue function.

Now, this code works. I kinda know how static variables work, so I'm just really confused as to why the previous code doesn't work - it throws a nil value not found error.

The originalVC:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == K.latinSegue {
        if latinTextField.text != nil && latinTextField.text != "" {
            guard let latinText = latinTextField.text else { fatalError("latinText broken")}
            LatinViewController.latinText = latinText
        }
    }
}

The destinationVC:

static var latinText: String!

@IBOutlet weak var textLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
    let latinTranslator = LatinTranslator()
    let latinDefinition = latinTranslator.getHTMLAndDefinition(latinText: LatinViewController.latinText)
    textLabel.text = latinDefinition
}

The segue is called when a button is pressed. I have checked, there is only one segue. Please help!

AdamPro13

The issue you’re having is that you’re creating a new instance of LatinViewController and not using the instance that your app will actually be navigating to.

Instead of this:


override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == K.latinSegue {
        if latinTextField.text != nil && latinTextField.text != "" {
            guard let latinText = latinTextField.text else { fatalError("latinText broken")}
            let destinationVC = LatinViewController()
            destinationVC.latinText = latinText
            
        }
    }
}

You should be doing something like this:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
   guard segue.identifier == K.latinSegue, let destination = segue.destination as? LatinViewController else {
      return
   }
   guard let text = latinTextField.text, !text.isEmpty else {
      return
   }
   destination.latinText = text
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why does my variable resets it's value?

Why does Outlook revert my change to a meeting and claim it is "out of date"?

Why does my SwiftUI animation revert to the previous frame on rotation?

Why does my transaction revert when calling another contract function?

Why does my function not modify the value of variable in a struct in C?

Why does a variable in my struct not hold its value?

Why does my function not return value to global variable?

Why does my variable return null instead of the defined value

Why does my code print out text and not the value of the variable?

Revert a variable into its original value

Why does my program enters into an infinite loop when my char variable reaches the [del] character (127) value?

Why does the value of this variable change?

Why does the value of the variable change?

why does variable not update value

Why does value of a variable not change

Why does my variable not work, but the definition does?

Why does assigning a MySQL pool to a member variable revert to undefined when a member function is called?

Why is my Swift string variable returning nil?

JavaScript - How to modify 'variable' inside function so that it does not revert to original value after function termination?

Why does my uninitialised variable become unassigned again after it has its value set inside of TryParse?

Why does my optimised IL store the same value in the save variable twice?

Why does in my case value typed variable slower than reference type?

Why does my WebSocket message handler always get React state hook variable's initial value?

Why does my C function return string variable as a pointer instead of a value?

Why does this C code change my variable lastName to another value without telling it to?

Why does my const reference variable, assigned via a getter that returns a const reference, not have the desired value?

Why does it say that my variable is not defined?

why my variable does not pass in the modal box

Why does my variable resut in an overflow in Java?