Эй оригинальное яблоко гид в Obj-C здесь,Swift NSTableView конвертировать 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!
}
Я сделал эти два изменения, но столкнулся с проблемой в статусе возврата «Невозможно преобразовать тип выражения« NSTextField »? для ввода '()' 'идентификатор действительно работает сейчас. – nsij22
Это потому, что вы не указали тип возвращаемого значения в объявлении метода, поэтому он ожидает пустой кортеж (т. Е. 'Void'), что означает отсутствие возвращаемого значения – Antonio
См. Обновленный ответ – Antonio