index rss mastodon twitter github linkedin email
Álvaro Ramírez
sponsor

Álvaro Ramírez

02 December 2018 Swift nil-coalescing operator

Paul Hudson, over at Hacking with Swift, has written The Complete Guide to Optionals in Swift. One of the many highlights is the nil-coalescing operator. If you're a fan of the C-like syntax in ternary operations, you'd enjoy chaining with Swift's nil-coalescing operator:

let players = [ "goose": "run!" ]
let move = players["duck1"] ?? players["duck2"] ?? players["duck3"] ?? players["goose"]
print("\(String(describing: move))")
Optional("run!")

ps. Swift snippet run on Emacs org babel's ob-swift. See Multiline Swift strings for details.