【发布时间】:2014-10-24 07:08:31
【问题描述】:
嘿,obj-c 中的原始苹果指南在这里,
示例 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!
}
【问题讨论】: