NSTableView - how to get automatic and manual sorting working together?

630 views Asked by At

I have a NSTableView in MyViewController and have an array controller bound to it. I want to be able to sort it by clicking the table column headers and it should also auto-sort. So for auto-sort I set the Sort Descriptors binding of my array controller in IB as following:

Bind to: MyViewController
Model Key Path: customSortDescriptors

In the MyViewController class I've added:

var customSortDescriptors:[NSSortDescriptor] {
    let sd = NSSortDescriptor(key: "string", ascending: true, selector: "localizedStandardCompare:");
    return [sd]
}

And for the related table column in IB I've set:

Sort Key: string
Selector: caseInsensitiveCompare:

This works OK until I click on the table column header upon where I get an error thrown:

Error setting value for key path customSortDescriptors of object NSAutounbinder: 0x60000005e060 - a nonretaining proxy for MyApp.MyViewController: 0x6000000e6600 (from bound object NSArrayController: 0x6000001c0b40[entity: GeneratedData, number of selected objects: 0]): [MyApp.MyViewController 0x6000000e6600 setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key customSortDescriptors.

I'm not sure why I get this error. Can somebody tell me what is wrong? Why would MyViewController not be key value coding-compliant? Aren't NSViews and NSViewControllers compliant by default?

Besides that the sorting works without problems but I get the error thrown in Xcode and want to eliminate that.

1

There are 1 answers

1
BadmintonCat On BEST ANSWER

I will be damned but the reason for the error is obviously that the property is read-only. So adding a setter, or better yet, change the code to simply:

var customSortDescriptors = [NSSortDescriptor(key: "string", ascending: true, selector: "localizedStandardCompare:")];

fixed the error and both, automatic and manual sorting works.