I'm using a MacBook Air with macOS10.14.5 and Xcode 10.2.1. When testing the code below, why won't the default "Unknown" appear in LblMessage when no text has been entered in TxtName and the button is pushed?
There's a difference between nil (ie. no string), and "" (ie, the empty string).
You could either test for the empty string, or maybe even create a convenience on strings through an extension:
extension StringProtocol {
var nonEmpty: Self? {
return isEmpty ? nil : self
}
}
let name = textfield.text.nonEmpty ?? "Unknown"
label.text = "Hello " + name