0
votes

Hey the original apple guide in obj-c is here,

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

example 3.2

I have been following through the guide and can not get the program to work, i am getting these errors:

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

and

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

What am i doing wrong?

 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

}

New Working code

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!
}
1

1 Answers

1
votes

Here:

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

you have declared the result variable as an optional AnyObject - and this protocol doesn't have an identifier property, which is instead a property of NSTextField.

What you should do is declare that variable using the correct type:

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

which can also be shortened thanks to type inference:

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

Side note: I think your if is checking for the opposite condition:

if result != nil

whereas I think it should be:

if result == nil

Side note 2

There's no return value declared in your function, but you are returning an instance of NSTextField. You should change the signature to:

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

and change the return statement to:

return result!

Note that result is defined as optional, but looking at the method implementation, at the end a non-nil value is available, so I've used forced unwrapping and declared the method to return a non-optional value. Of course feel free to change that if you want.