Swifty
Swifty

Reputation: 65

Swift optionals, unwrapping

I am learning the swift syntax and got a little confused with the optional type. So, from the defination a swift type can not store the null value unless it is explicitly defined as an optional. So, what does the variable in the following line contains when it's declared.

var a:Int  (declaring a variable without intializing it working fine in swift 3)

And also when we declare a variable as an optional with a "!",

var optionalSquare: Square! = Square(sideLength: 10, name: "Optional Square")

if we want to use the "optionalSquare" variable, we do not need to unwrap it cause we are sure(I think that is why we are using "!" instead of "?") it does not contain any null value. so why don't we declare it as a normal variable.

Please correct any false statements. Thank you.

Upvotes: 1

Views: 139

Answers (3)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726579

So, what does the variable in the following line contains when it's declared.

It does not matter, because you cannot access it anyway. A declaration like this tells Swift that you are going to decide on the value later. Reading the variable before assigning is an error that the compiler is going to catch.

why don't we declare [variables with forced unwrapping] it as a normal variables?

Because you may want to store nil in that variable at the times when it should not be used.

For example, you should make IB-assigned property implicitly unwrapped, because they need to be nil before NIB objects are "inflated", but after the init method has completed:

@IBOutlet private weak var myLabel: UILabel!

In order to make myLabel non-optional you would have to drop weak. On the other hand, you don't want to use exclamation point on each access of myLabel, because you know for sure that it must be initialized.

The same reasoning applies to other variables that you would like to make non-optional, but cannot assign in an initializer.

Upvotes: 1

Antonio
Antonio

Reputation: 72760

You can declare a non optional variable and not initialize it - the compiler doesn't complain as long as you do not use that variable. So this code

var num: Int

won't generate any error. However if you reference that variable before initializing, compilation fails

var num: Int
print(num)

In the former case there's no error because initialization can be deferred. You can declare a variable, and then initialize 100 lines after. As long as you don't reference it before initializing, you're ok.

To answer your second question, it's correct to say that in many cases it doesn't make much sense declaring and contextually initializing an implicitly unwrapped variable: a non optional variable is more appropriate.

Upvotes: 0

jscs
jscs

Reputation: 64002

what does [var a:Int] contains when it's declared

Nothing. Its value is undefined. Using it before assigning a value is a compilation error. You can declare it without initializing it, but you cannot use it.

This is part and parcel of Swift's philosophy of safety: in C, you could likewise leave a variable uninitialized, and its value would be undefined, but the compiler would not (by default) report error if you use it.

so why don't we declare it as a normal variable.

There's no reason that I can think of to make a local variable an implicitly unwrapped optional. The functionality is intended for properties ("ivars") on structs or classes. You should use them where it's impossible to set the property in the object's init, but it is certain that the value will be present before the object is used.

IBOutlets are probably the canonical use case. Another use that I find helpful is to allow setting a property by calling a method in init.

Upvotes: 2

Related Questions