I'm attempting to set a text label string value equal to the text field string value. However, it is crashing due to a unexpectedly found nil while unwrapping an optional value. I thought that nil-coalescing will work just fine in a macOs application. Any idea what would be the proper way to handle this error?
At the moment, I have tried to nil-coalescing to make sure that the default value is a blank string.
@IBAction func textFunctionality(_ sender: NSButton) {
let text: String = textField.stringValue ?? ""
switch sender.tag {
case 0:
print(textLabel.stringValue)
textLabel.stringValue = text
default:
print("error")
}
}
I expect to set the text label string value to the text field string value. For example, if the string value of the text field is Adam, then string value of the text label will be Adam as well.