Cannot form weak reference to instance of class NSTextView

swift

Solution

Use `@IBOutlet var scrollView: NSScrollView` instead of `@IBOutlet var textField: NSTextView`. Then create a property returns documentView in scrollView.

import Cocoa

class AppDelegate: NSObject, NSApplicationDelegate {

    @IBOutlet var window: NSWindow
    @IBOutlet var scrollView: NSScrollView

    var textField: NSTextView {
        get {
            return scrollView.contentView.documentView as NSTextView
        }
    }

    @IBAction func displaySomeText(AnyObject) {
        textField.insertText("A string...")
    }

    func applicationDidFinishLaunching(aNotification: NSNotification?) {
        // Insert code here to initialize your application
    }

    func applicationWillTerminate(aNotification: NSNotification?) {
        // Insert code here to tear down your application
    }
}

Problem

Using Swift only, here's my code in AppDelegate.swift: ``` import Cocoa class AppDelegate: NSObject, NSApplicationDelegate { @IBOutlet var window: NSWindow @IBOutlet var textField: NSTextView @IBAction func displaySomeText(AnyObject) { textField.insertText("A string...") } func applicationDidFinishLaunching(aNotification: NSNotification?) { // Insert code here to initialize your application } func applicationWillTerminate(aNotification: NSNotification?) { // Insert code here to tear down your application } } ``` In the interface builder, I have an object hooked up to receive input from a button, then the output goes to a text view. I'm trying to get the text view to populate with some text when I hit the button. I tried this with a text field as well, and didn't get the error, but got a "dong" error sound and it didn't do anything else. In Objective-C, you had to use the `(assign)` parameter to get this to work from what I understand. What am I doing wrong?

Original source

Related problems