Skip to content

Instantly share code, notes, and snippets.

@yakushevichsv
Created September 22, 2021 19:42
Show Gist options
  • Save yakushevichsv/a224b24378a1b4abcb72d015a609b9a7 to your computer and use it in GitHub Desktop.
Save yakushevichsv/a224b24378a1b4abcb72d015a609b9a7 to your computer and use it in GitHub Desktop.
Simple sample of using Map & FlatMap On Optionals
var str: String?
let result0 = str.flatMap { Int($0) }.valueOrDefault(.min)
var result1: Int = .min
if let str = str {
result1 = Int(str) ?? .min
}
// result1 can be changed here, but result0 can't.
assert(result0 == result1)
//Similar can be done using "map" method
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment