Tharnatoz Tharnatoz - 4 months ago 13
Swift Question

UITextView.text can't take a variable of type String?

There are very confusion things going on...

I try to set a text from the UITextView via

UITextView.text = myVar
but this won't work, every time i run this code this exception is shown:


fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)


@IBOutlet weak var myTextView: UITextView!

let myStringVar: String = "I am a test string"

myTextView.text = myStringVar


But this peace of code works very well:

@IBOutlet weak var myTextView: UITextView!

myTextView.text = "I am a test string"


Why is that so? Can anyone help me?

Answer

insert print(myTextView) before myTextView.text = myStringVar and check, if myTextView not nil.