Switch Swift operator for a tuple of optional booleans

I am having trouble figuring out how to use options inside a tuple inside a switch. The syntax below (let ...) ... works like not a tuple, but inside the tuple I get the expected separator material: (

var dict = Dictionary<String,Bool>() dict["a"] = true switch (dict["a"],dict["b") { case (.Some(let a) where !a, .Some(let b) where b): println("false/nil, true") case (.Some(let a) where a, .Some(let b) where !b): println("true, false/nil") 

I want to avoid the following

  if let a = self.beaconList["a"] { if let b = self.beaconList["b"] { // a, b } else { // a, !b } } else { if let b = self.beaconList["b"] { // !a, b } else { // !a, !b } } 
+7
switch-statement tuples swift optional
source share
2 answers

There are several ways to do this, but to correct the syntax of what you are trying to do literally, you need to explicitly remove Optional, either by matching with .Some (false), or by deploying it! (I think this is strange, as you will see)

 var dict = Dictionary<String,Bool>() dict["a"] = true dict["c"] = false func matchOneOrTheOtherWithOptionals(a: Bool?, b: Bool?) -> String { switch (a, b) { case (.Some(true), let b) where b == .None || !b!: // gross return "a was true, but b was None or false" case (let a, .Some(true)) where a == .None || a == .Some(false): return "a was None or false and b was true" default: return "They both had a value, or they were both missing a value" } } matchOneOrTheOtherWithOptionals(true, .None) // "a was true, but b was None or false" matchOneOrTheOtherWithOptionals(true, false) // "a was true, but b was None or false" matchOneOrTheOtherWithOptionals(.None, true) // "a was None or false and b was true" matchOneOrTheOtherWithOptionals(false, true) // "a was None or false and b was true" matchOneOrTheOtherWithOptionals(false, false) // "They both had a value, or they were both missing a value" matchOneOrTheOtherWithOptionals(true, true) // "They both had a value, or they were both missing a value" matchOneOrTheOtherWithOptionals(.None, .None) // "They both had a value, or they were both missing a value" 

You can also try the following:

 func noneToFalse(bool: Bool?) -> Bool { if let b = bool { return b } else { return false } } func matchOneOrTheOther(a: Bool, b: Bool) -> String { switch (a, b) { case (true, false): return "a is true, b was false or None" case (false, true): return "a was false/None, b was true" default: return "both were true, or both were false/None" } } matchOneOrTheOther(noneToFalse(dict["a"]), noneToFalse(dict["b"])) 

Here is the gist of the playground that I used when writing this answer: https://gist.github.com/bgrace/b8928792760159ca58a1

+13
source share

Simplify!

 var dict = Dictionary<String,String>() dict["a"] = "the letter a" switch (dict["a"],dict["b"]) { case (.None, let b): println("false/nil, true \(b)") case (let a, .None): println("true, false/nil \(a)") default: println("don't know") } 
+2
source share

All Articles