Skip to content

Instantly share code, notes, and snippets.

@yannxou
Created February 7, 2019 15:36
Show Gist options
  • Select an option

  • Save yannxou/6701991f894da7ae534fe46e36766601 to your computer and use it in GitHub Desktop.

Select an option

Save yannxou/6701991f894da7ae534fe46e36766601 to your computer and use it in GitHub Desktop.
Join array of optional strings (the functional way)
struct Data {
let name: String?
let location: String?
var components: String {
return [name, location]
.compactMap { $0 }
.filter { !$0.isEmpty }
.joined(separator: " - ")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment