I made a sample application displaying list of employees with their salaries over NSTableView using NSArrayController.
Basic functionality is working as intended. Also sorting is working fine when Selector - compare
or caseInsensitiveCompare
is assign开发者_Go百科ed in attribute pane for table column- employeeName
.
Problem occurs when I try to sort it by string length by setting selector in IB as - length
, for the table column - employeeName
. It does sorting by string length but does it inappropriately.
It is doing sorting on alternate clicks
ie. on first click it sorts the content in table in descending order by length. Then on second click it does not arrange contents in ascending order by length but sets arrow on top as in ascending. Then on third click it arranges contents in ascending order by length but sets arrow on top of table column as in descending and so on...
Can anyone suggest if I am wrong somewhere?
Thanks,
Miraaj
I'm going to guess you are working your way through Cocoa Programming for Mac OS X Chapter 8,
You need to set the sortKey to personName.length
and the selector to compare:
for the column.
Then the sort descriptor will sort by 'comparing' the string 'lengths'.
Answer for beginners of macOS development(reading BNR's book) and Swift language:
Swift string doesn't have a length
property. You can access its length by the count of its character array.
To achieve this you should set the sortKey
to name.characters.count
and selector
to compare:
.
I guess BNR is suggesting that you should find something to represent the length of a string, rather than there is a length
property or method.
精彩评论