-8

iam very new to the swift programming.i have a wrote a basic program in swift. i confused about the unwrapped optional concept and in this program i have already declared celsius and fahrenheit as unwrapped optional by default. my question is

Why do we need to unwrap again as celsius.text!

can you please provide solution for this...Thank you

PROGRAM CODE

Saiteja
  • 71
  • 10
  • 5
    Please post you code in your question and not link to an image! – rckoenes Dec 15 '16 at 15:36
  • 3
    here you have an explanation about optionals http://stackoverflow.com/questions/24003642/what-is-an-optional-value-in-swift – Ponja Dec 15 '16 at 15:37
  • 1
    Learn from [the source](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-ID330). The Swift manual is very well written and easy to follow. // Please never post an *image* of code, this makes no sense. Always post the code itself, as text, in the question. Use the [edit] button to add the code. – Eric Aya Dec 15 '16 at 15:48
  • Possible duplicate of [Why create "Implicitly Unwrapped Optionals"?](http://stackoverflow.com/questions/24006975/why-create-implicitly-unwrapped-optionals) – NobodyNada Dec 15 '16 at 18:55

3 Answers3

2

You have non-optional UITextField, but its property text is String? type, you must unwrap it like:

if let text = celsius.text {
    print(text)
}
Artem Novichkov
  • 2,356
  • 2
  • 24
  • 34
  • Or you can just use ! if you are sure that the .text isn't nil – Ponja Dec 15 '16 at 15:40
  • `UITextField` is declared as non-optional `IBOutlet` but `text` property of `UITextField` is an optional `String?`. Hence you need to do an optional un-wrap to access it's `text` value. – Gurdev Singh Dec 15 '16 at 16:05
0

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.)

Duncan C
  • 128,072
  • 22
  • 173
  • 272
0

You can do it like that. Try to avoid using force unwrapping (! operator) on optionals.

func conversion() {

    if ( celsius.text == "" ) {
        if let fahrenheitText = fahrenheit.text, let fahrenheitValue = Double(fahrenheitText) {
            let celsiusValue = fahrenheitValue - 32 * 5 / 9 //or whatever is the formula..
            celsius.text = "\(celsiusValue)"
            fahrenheit.text = ""
        }
    } else if ( fahrenheit.text == "" ) {
        if let celsiusText = celsius.text, let celsiusValue = Double(celsiusText) {
            let fahrenheitValue = 9 * celsiusValue / 5 + 32 //or whatever is the formula..
            fahrenheit.text = "\(fahrenheitValue)"
            celsius.text = ""
        }
    }

}
Stefan Nestorov
  • 350
  • 6
  • 14