Assign text in "Text View" to a variable makes app crash

basically when the code below runs, the app crashes and I can’t figure out why. I think I may be assigning the text inside of “test1” to the variable “new1var” in the wrong way, but I’m not sure.

@IBOutlet weak var test: UITextField! //text field
@IBOutlet weak var test1: UITextView! //text view


@IBAction func submit(_ sender: Any) { //when a button is pressed

    let newvar = test.text
    let new1wvar = test1.text     //this line is where it crashes
}

Thank you so much for your help (I’m new to swift)!

App crash because test1.text maybe nil. UITextView’s text or UITextField’s text is nil at the first time it’s initialized.

To resolve this problem:

Replace this line

let new1wvar = test1.text

by:

let new1wvar = test1.text ?? "" // If test1.text is nil, then will use "" to asign

The line

@IBOutlet weak var test1: UITextView!

says that test1 is an implicitly unwrapped optional. So compiler would allow you to access it without unwrapping it but its your responsibility to make sure that before accessing it, its not nil.

Make sure that you have properly linked test1 to your Storyboard/XIB.

After making sure that it is linked properly in other answer give some default value if test1.text is nil.

let new1wvar = test1.text ?? ""

Try This

@IBOutlet weak var test: UITextField!
@IBOutlet weak var test1: UITextField!


@IBAction func submit(_ sender: Any) {
     guard let  newvar = test.text ,let new1wvar = test1.text else{
        print("Error")
        return
     }
     print(newvar)
     print(new1wvar)
}