0

When I want to use a guard to make sure a double-optional value is not nil, what are my options?

let something: Bool?? = true

guard let anything: Bool = something else {
  return
}
Jawad Ali
  • 13,556
  • 3
  • 32
  • 49
Musection
  • 135
  • 6

1 Answers1

4

You can use Guard as well as if let

if let getValue = something, let value = getValue {
    print(value) 
}

guard let getValue = something , let value = getValue else {
  return
}

You can also use FlatMap

if let value = something.flatMap({ $0 }) {
    print(value)  
}

If you have any level of optionals e.g 3,4,5,7 you will get the value with Conditionally cast

  let something: Bool????? = true
    if let value = something as? Bool {
        print(value) // prints true
    }
Jawad Ali
  • 13,556
  • 3
  • 32
  • 49