【问题标题】:Swift NSTableview convert obj-c to swiftSwift NSTableview 将 obj-c 转换为 swift
【发布时间】:2014-10-24 07:08:31
【问题描述】:

嘿,obj-c 中的原始苹果指南在这里,

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/TableView/PopulatingView-TablesProgrammatically/PopulatingView-TablesProgrammatically.html#//apple_ref/doc/uid/10000026i-CH14-SW6

示例 3.2

我一直按照指南进行操作,但无法让程序运行,我收到以下错误:

'AnyObject?' does not have a member named 'identifier'
 line: result.identifier = "HelloWorld"

error: cannot convert the expression's type 'AnyObject?' to type '()'
Line: return result

我做错了什么?

 func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn, row: Int){
    var names: [String] = ["Anna","Alex","brain","jack","gg"]
    var result: AnyObject? = tableView.makeViewWithIdentifier("HelloWorld", owner: self)

    if result == nil
    {
        // Create the new NSTextField with a frame of the {0,0} with the width
        // of the table.
        result = NSTextField(frame: NSRect())
        // set identifier of the NSTextField instance to HelloWorld.
        result.identifier = "HelloWorld"

    }

    result = names[row]
    return result

}

新的工作代码

func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn, row: Int) -> NSTextField{


    var names = ["Anna","Alex","Brain","Jack","Hello"]
    var result: NSTextField? = tableView.makeViewWithIdentifier("HelloWorld", owner: self) as? NSTextField        
    if result == nil
    {
        // Create the new NSTextField with a frame of the {0,0} with the width
        // of the table.
        result = NSTextField(frame: NSRect())
        // set identifier of the NSTextField instance to HelloWorld.
        result?.identifier = "HelloWorld"

    }
    result!.bezeled = false
    result?.stringValue = names[row]
    return result!
}

【问题讨论】:

    标签: macos cocoa swift


    【解决方案1】:

    这里:

    var result: AnyObject? = tableView.makeViewWithIdentifier("HelloWorld", owner: self)
    

    您已将 result 变量声明为可选 AnyObject - 此协议没有 identifier 属性,而是 NSTextField 的属性。

    您应该做的是使用正确的类型声明该变量:

    var result: NSTextField? = tableView.makeViewWithIdentifier("HelloWorld", owner: self) as? NSTextField
    

    由于类型推断,也可以缩短:

    var result = tableView.makeViewWithIdentifier("HelloWorld", owner: self) as? NSTextField
    

    旁注:我认为您的 if 正在检查相反的情况:

    if result != nil
    

    而我认为应该是:

    if result == nil
    

    旁注2

    您的函数中没有声明返回值,但您返回的是NSTextField 的实例。 您应该将签名更改为:

    func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn, row: Int) -> NSView
    

    并将return语句更改为:

    return result!
    

    请注意,result 被定义为可选,但查看方法实现,最后一个非 nil 值可用,因此我使用了强制展开并声明该方法返回一个非可选值。当然,如果您愿意,可以随意更改。

    【讨论】:

    • 我做了这两项更改,但在返回语句中遇到了问题“无法转换表达式的类型 'NSTextField?'输入 '()'" 标识符现在可以工作了。
    • 那是因为你没有在方法声明中指定返回类型,所以它需要一个空元组(即Void),这意味着没有返回值
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多