Swift Snippet : Map Bool values with Generics in Swift

My problem

It often occurs while writing code that you need to assign a value of a certain type, based on the value of Boolean. To do this there are multiple ways, but the 2 most known are

1. The let’s-write-a-lot-of-code approach

let isAWildBoolean = true

// Set the value of `offSet` to a value based on the Boolean
let offSet:Int

if isAWildBoolean {
    
    offSet = 10
    
} else {
    
    offSet = 20
    
}

I don’t like the first approach, because it creates a bloated codebase. A better way is to use the ternary operator (also known as the inline-if).

2. The one-line-star-developer (ternary operator) approach

You specify the Boolean you want to evaluate, add a question mark, followed by the value if the Boolean is true, followed by a colon, followed by the value if the Boolean is false.

Continue reading Swift Snippet : Map Bool values with Generics in Swift