The easiest and most readable option is likely to use if let, but you can also determine the method prependa String, the opposite String.append, and then use an optional chain:
extension String {
func prepending(prefix: String) -> String {
return prefix + self
}
}
var name: String?
let username = name?.prepending("@")
source
share