Proper way to concatenate optional swift strings?

compactMap would work well here, combined with .joined(separator:):

let f: String? = "jo"
let l: String? = "smith"

[f,l] // "jo smith"
  .compactMap { $0 }
  .joined(separator: " ")

It doesn’t put the space between if one is nil:

let n: String? = nil

[f,n] // "jo"
  .compactMap { $0 }
  .joined(separator: " ")

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)