I'm doing something similar to:
@IBOutlet var textView: NSTextView!
override func viewDidLoad() {
super.viewDidLoad()
textView.delegate = self
}
func textDidChange(notification: NSNotification) {
}
I did add "NSTextViewDelegate" at the very beginning.
I want to set the string value of a label if the text in the "textView" is changed.
The problem is that my program will just crash without any error message (breakpoint is at "fun textDidChange()......"). It will crash even if the func textDidChange is empty.
Am I doing anything wrong ?
Thanks
Use this, Swift 3
class ViewController: NSViewController, NSTextViewDelegate {
@IBOutlet var textView: NSTextView!
override func viewDidLoad() {
super.viewDidLoad()
textView.delegate = self
// NSTextViewDelegate conforms to NSTextDelegate
func textDidChange(_ notification: Notification) {
guard let textView = notification.object as? NSTextView else { return }
print(textView.string)
}
}
Remember
_
in textDidChange(_ notification: Notification)
Notification
instead of NSNotification
textView.delegate = self
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With