无法形成对类NSTextView实例的弱引用

Ste*_*eve 11 swift

仅使用Swift,这是我在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
    }


}
Run Code Online (Sandbox Code Playgroud)

在界面构建器中,我有一个连接的对象来接收来自按钮的输入,然后输出转到文本视图.当我按下按钮时,我正试图让文本视图填充一些文本.

我也用文本字段尝试了这个,并没有得到错误,但是听到了"dong"错误,它没有做任何其他事情.在Objective-C中,你必须使用(assign)参数来使我的理解起作用.

我究竟做错了什么?

Joe*_*oel 15

NSTextView由于Cocoa和AppKit的历史问题,您无法存储弱引用.请参阅Clang文档中的详细信息. NSTextViewNS_AUTOMATED_REFCOUNT_WEAK_UNAVAILABLENSTextView.h中标记,还有一些其他类查找.

您是否尝试过Swift无主引用而不是弱引用,这有点像Objective-C的赋值(您NSTextView在Objective-C中用于插座的那些)?


tag*_*gon 12

@IBOutlet var scrollView: NSScrollView而不是@IBOutlet var textField: NSTextView.
然后在scrollView中创建一个返回documentView的属性.

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
    }
}
Run Code Online (Sandbox Code Playgroud)