2

When will a variable be inferred as an implicitly unwrapped optional by Swift compiler? Or the variable has to always be declared with ! for it to be treated as such?

Boon
  • 40,656
  • 60
  • 209
  • 315
  • 3
    As far as I know, only `IBOutlets` are inferred to be implicitly unwrapped. Everything else needs to be declared with a `!`. The reason its called "implicitly unwrapped" is because you can access properties of it without unwrapping it first. Other than that it behaves identically to Optionals – Jack Jun 24 '14 at 12:57
  • Thanks Jack, useful bit on IBOutlet. As far as it behaving identically as Optional, it can't contain nil though, right? – Boon Jun 24 '14 at 12:59
  • 1
    Yes it can, it is an "implicitly unwrapped Optional" which means its an Optional - just that when you access the properties, you don't have to unwrap it first. Thats about it.. – Jack Jun 24 '14 at 13:06
  • 1
    Thanks Jack, can implicitly unwrapped optional participate in optional chaining like normal optional, using ? – Boon Jun 24 '14 at 13:07
  • 1
    Yes, like I said it behaves exactly like any other optional with one added difference – Jack Jun 24 '14 at 13:33

1 Answers1

1

The type inference occurs at compile time. The only way for a variable to be inferred as an implicitly unwrapped optional is to assign one to it (either directly, or by assigning the return value of a function, which returns an implicitly unwrapped optional).

Basically, the "optional-ness" of a variable is part of it's type. The type of the following variables, x and y is implicitly unwrapped Optional Int (for both), and most importantly, the type system does not consider this the "same type" as Int. (Though you can use it interchangeably with an Int as long as it is not nil)

let x: Int! = 7

fund make_y() -> Int! {
  return 7
}

let y = make_y()
Jiaaro
  • 74,485
  • 42
  • 169
  • 190
  • It's odd that it's called implicitly unwrapped when you have to specify !, it would seem like it should be called explicitly unwrapped? – Boon Jun 24 '14 at 12:55
  • You're explicit about it in the declaration, but throughout your code the unwrapping is implicit - no need for ! or ?. – Nate Cook Jun 24 '14 at 12:58
  • 1
    It's called implicitly "unwrapped". In swift only optionals can be nil. Normally you have something like `var x: Int? = 7` and to get at the `7` you need to write `x!` (to unwrap it). by using the "implicitly unwrapped optional" you don't have to write `x!` to access the `7`, you just write `x`. I wrote a detailed explanation of the differences on [this question](http://stackoverflow.com/a/24122706/2908) – Jiaaro Jun 24 '14 at 12:59
  • @Boon (forgot to mention you in the last one, oops!) To be "explicitly wrapped", it would be `Int?` (where you cannot access the value without using the forced unwrapping operator (i.e., `x!`) – Jiaaro Jun 24 '14 at 13:35
  • Thanks Jiaaro. ? is Optional under the hood, what about implicitly unwrapped optional - is there a representation for it? – Boon Jun 24 '14 at 13:41
  • @Boon I don't know, sorry - It's possible that it also uses `Optional`, but with an annotation to tell the compiler to automatically unwrap it. – Jiaaro Jun 24 '14 at 14:29