Steve
Steve

Reputation: 592

Cannot form weak reference to instance of class NSTextView

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?

Upvotes: 11

Views: 8220

Answers (6)

Joel
Joel

Reputation: 2295

You cannot store a weak reference to an NSTextView due to historical issues with Cocoa and AppKit. See details in the Clang documentation. NSTextView is marked as NS_AUTOMATED_REFCOUNT_WEAK_UNAVAILABLE in NSTextView.h, there are also a few other classes to lookout.

Have you tried a Swift unowned reference instead of weak, which is kind of like Objective-C's assign (what you'd use for an NSTextView outlet in Objective-C)?

Upvotes: 15

taggon
taggon

Reputation: 1936

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
    }
}

Upvotes: 12

alondono
alondono

Reputation: 2701

I have tried to replicate what you described. I have created a new OS X app using Xcode 6 beta 7. I have dropped a button and text view in the main form.

I think your problem is that the connection to the Text View object is not correct for some reason. To make things easier, I've connected the objects using control-drag, which adds the required code automatically. So first I've connected the Text View. To do this click on the text view object until Text View is selected. When I do this in my version of Xcode, the first time I click on the object, Bordered Scroll View is selected. Clicking on it again then selects Clip View. Finally, clicking on it again selects Text View. Now I control-drag from the object to the AppDelegate.swift code (It helps to display the Assistant Editor so that you have your form UI and code side-by-side).

By doing this I get this little window:

enter image description here

Notice that the type is NSTextView and the storage is Weak. I've only had to add the name and click Connect. This adds the following code in AppDelegate.swift:

@IBOutlet var textField: NSTextView!

The code is almost exactly like the one you have, except for the ! at the end of the line, which forces to unwrap the value of textField.

Just with that, the code as you have it in your question should work.

The other thing I would suggest is not to use insertText. According to Apple's documentation for NSTextView.insertText:

This method is the entry point for inserting text typed by the user and is generally not suitable for other purposes. Programmatic modification of the text is best done by operating on the text storage directly.

As far as I understand this, programmatic modification of the text by operating on the text storage directly means dealing with NSText, which NSTextView inherits from. So instead, use NSText.string. This is how the click button action looks in my code:

@IBAction func displaySomeText(sender: NSButton) {
    // If you want to add a new 'A string... ' every time you click the button
    textField.string! += "A string... "
    // otherwise just use
    //textField.string = "A string..."
} 

I have added the Button Action in the same way as I've added the Text View Outlet, by control-dragging, and, in this case, selecting NSButton as the sender, instead of leaving the default AnyObject.

Upvotes: 1

Andrew Ebling
Andrew Ebling

Reputation: 10283

Does the "dong" error suggest a responder chain problem? What if you call becomeFirstResponder on the text field before inserting the text?

Upvotes: 0

Will Fancher
Will Fancher

Reputation: 245

@IBOutlet automatically makes a property weak IIRC, but weak doesn't automatically make a property optional. But it is required that a weak property be made optional, as the property could at any time be deallocated and made nil. So you have to declare your @IBOutlets as optional.

import Cocoa

class AppDelegate: NSObject, NSApplicationDelegate {

    @IBOutlet var window: NSWindow? // Optional
    @IBOutlet var textField: NSTextView?

    @IBAction func displaySomeText(AnyObject) {
        textField?.insertText("A string...") // Only call the method if the object exists
    }

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

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

Upvotes: 0

Daniel
Daniel

Reputation: 577

To create a weak reference user the weak keyword:

example:

@IBOutlet weak var myView: UIView

In your case

@IBOutlet weak var textField: NSTextView

Upvotes: -2

Related Questions