macoscocoaswiftnstablecolumn

How do I set an NSTableColumn headerCell stringValue in swift?


The documentation makes it look as if it would be as easy as this:

var tc = NSTableColumn(identifier: "mycolumn")
tc.headerCell.stStringValue("foo")

The last line of code is a compile error, which I don't understand. On top of that I get a couple of different compile error message for this exact same line of code depending on what mood XCode seems to be in. I've seen the following compile errors:

  1. AnyObject does not have a member named 'stStringValue'
  2. Cannot convert the expression's type 'NSString' to type 'StringLiteralConvertible'

I get this when try setting a variable and putting that in there:

  1. Cannot convert the expression's type 'String' to type 'String?!'

I get this when I try the "foo \(bar)" variant:

  1. Could not find member 'convertFromStringInterpolationSegment'

Hilariously enough if I declare a variable with type String?! it says it Cannot convert the expression's type 'String?!' to type 'String?!'

I don't understand what is going on. Is this a bug?

If I try this in a playground, the playground actually wants to autocomplete with an extra argument, this format:

tc.headerCell.setStringValue("foo", resolvingEntities: false)

This has no compile time errors, but results in a seg fault at run time. This format is also not in the documentation.

I really have no idea what's going on and the error messages aren't helping.


Solution

  • I finally got this to work, despite the terrible error messages. The problem is that headerCell is typed as AnyObject, I had to cast it. The second problem was that in swift land it's a property, not a method.

    This finally worked:

    var tc = NSTableColumn(identifier: "mycolumn") 
    let hc = column1.headerCell as NSTableHeaderCell
    hc.stringValue = "foo"