9

I want to know about Bool in Swift.

If Bool is a basic primitive datatype, why is a Boolean's default value nil?

var test: Bool!
print(test) // nil

In Java the Boolean default value is false:

Default value of 'boolean' and 'Boolean' in Java

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gaurav Saini
  • 177
  • 1
  • 1
  • 8
  • 4
    Please link to the thing you're referring to instead of posting a picture of it. – molbdnilo Sep 11 '17 at 11:07
  • 4
    Note that `var test: Bool!` is no *yet* a Bool. The type is declared but the variable has no value. It is nil, therefore not true nor false. – Eric Aya Sep 11 '17 at 11:09
  • @Moritz Now I understand Thanks – Gaurav Saini Sep 11 '17 at 11:13
  • 2
    @GauravSaini, there's no such thing as `default value` for type in Swift, you must initialize each variable with specific value (except `Optional` which has it, and it's `nil`). – user28434'mstep Sep 11 '17 at 11:19
  • 1
    @user28434 `Void` has an implicit default value too; try `let v: Void; print(v)` – Hamish Sep 11 '17 at 11:23
  • 1
    @Hamish, ok `Void` is second exception, because it's a [`Unit`](https://en.wikipedia.org/wiki/Unit_type) type, therefore there's only one value option for it. – user28434'mstep Sep 11 '17 at 11:25
  • 1
    Adding to what @user28434 has already stated, `Void` is just a type alias to the empty tuple, `()`, this is why it has only one possible value. – Dávid Pásztor Sep 11 '17 at 12:50
  • -side note If you pull a Boolean from UserDefaults and it doesn’t have a set value, it will return false – JustAnotherGuy Apr 06 '20 at 02:08

3 Answers3

18

Bool, Bool! and Bool? all are different in Swift.

1. Bool is a non-optional data type that can have values - true/false. You need to initialize it in the initializer or while declaring it before using it.

var x : Bool = false

var x: Bool
init()
{
   x = false
}

2. Bool? is an optional data type that can have values - nil/true/false. In order to use this type, you need to unwrap it using if let or force unwrapping.

var x: Bool?

if let value = x
{
   //TODO: use value instead of x
}

3. Bool! is an implicitly unwrapped optional data type that can have values - nil/true/false. The difference here is it must contain a value before using it else it will result in runtime exception. Since it is implicitly unwrapped, no need to unwrap it using if let or force unwrapping.

var x: Bool! //Must contain value before using
PGDev
  • 23,751
  • 6
  • 34
  • 88
  • 3
    "*an implicitly unwrapped optional [...] it must contain a value before using it else it will result in runtime exception*" – actually since Swift 3, the line is real blurry with that; if an IUO can be type checked as a strong optional, it will be. For example, `var x: Bool!; print("\(x)")` will not crash; it will just output `nil`. – Hamish Sep 11 '17 at 11:36
  • thanks for the explanation! I'm still new to swift and it's sometimes tidies – Alex Cio Jul 21 '18 at 15:40
3

Strictly spoken there is no default value in Swift.

  • Either the Bool is non-optional then you have to assign a (default) value
  • or if the Bool is an optional, then it is nil – which is no value in terms of Swift.
pkamb
  • 33,281
  • 23
  • 160
  • 191
vadian
  • 274,689
  • 30
  • 353
  • 361
  • 1
    *Strictly spoken* there are default values for some intrinsic types in Swift. With `Optional`, while `nil` does indeed *express* "no value", it is a literal that represents the `.none` case of `Optional`, and is thus one of the possible values for it to have; and is not the same as being uninitialised. So `Optional` has an implicit default value of `.none`. It's not the only type with a default value; `Void` has a default value of `()`, `let v: Void; print(v)` is perfectly legal. (Although those are the only two types I can immediately think of with default values). – Hamish Sep 11 '17 at 14:16
0

Bool in Swift is not a primitive. Everything in Swift are objects. Your variable test is a Bool!, which is an implicitly unwrapped optional and the default value is nil.

If you use this code (not an optional),

var test: Bool
print(test)

You will get an error:

variable 'test' used before being initialized

So in Swift you have to initialize stuff before you use it, for example:

var test: Bool = false
Papershine
  • 4,995
  • 2
  • 24
  • 48
  • 4
    Saying that everything in "Swift" is an object is not good wording. `Bool` is a value type, which is what Java primitives are. You are right that every non-optional var needs to be initialized and optionals are `nil` by default. – Sulthan Sep 11 '17 at 11:21