You need to include the defintion of celsius
and fahrenheit
.
I'm guessing that they are UITextField
or UITextView
objects.
Those two view types have properties text
which are declared as optionals. So you have optional reference to a text field that contains a reference to an optional string.
So even though you declared celsius
as celsius: UITextField!
, the text
property is also an optional, so you need to say
celsius.text? = ""
Note that declaring a variable as an implicitly unwrapped optional is dangerous, because any time you reference that variable, the compiler will try to unwrap it for you (It says "This is an Optional, but trust me, it will never be nil.) This is like having an elevator where you push the button and the door opens, whether the car is there or not, and you step through without looking. If the car is there, great. If not, you fall to your death. You better be sure the elevator will always be there!
Outlets are one case where it's common to use implicitly declared optionals, because the outlets should be hooked up in IB (Interface Builder). If an outlet is not hooked up, you want to know about it right away, so crashing is reasonable.
Think of the !
operator as the "crash if nil" operator, and avoid it until you really understand optionals. (With the exception of outlets, as discussed above.)