无法调用非函数类型的值'AnyObject!?“Swift

时间:2016-02-25 11:56:57

标签: swift cocoa

我正在尝试修复此错误,但没有看到解决方案。 在其他有效的代码中!没意思!

支持类ScrolledString:在滚动的视图中打印字符串。

class ScrolledString: NSView{
var text: NSString = "";

init(frame frameRect: NSRect, string:NSString){
    self.text = string
    super.init(frame: frameRect)
}

required init?(coder: NSCoder) {
    self.text = ""
    super.init(coder:coder)
}

override func drawRect(dirtyRect: NSRect) {
    let font = NSFont(name: "Helvetica", size: 40.0)
    let attrs : [String: AnyObject] = [
        NSFontAttributeName: font!,
        NSForegroundColorAttributeName: NSColor.whiteColor()
    ]
    text.drawInRect(self.frame, withAttributes: attrs)
}

比在mainView中我试图添加ScrollView

let scrollStringContainer = NSScrollView(frame: NSRect(x: myRect.width*4/9, y: 0, width: myRect.width*8/9, height: myRect.height))
let view = NSView()
view.addSubview(ScrolledString(frame: scrollStringContainer.frame, string: self.name))
scrollStringContainer.documentView(view)

最后一行返回错误“无法调用nun-function type'AnyObject'的值。”我只是想尝试向scrollView添加视图

1 个答案:

答案 0 :(得分:0)

  

unowned(unsafe) var documentView: AnyObject?

documentViewAnyObject类型的可选属性,因此您无法像函数一样调用它。我不清楚你是否想使用contentView,如:

scrollStringContainer.contentView.addSubview(view)