item based NSBrowser


Gerriet M. Denkmann
 

On 4 Jan 2021, at 21:58, Bill Cheeseman <wjcheeseman@comcast.net> wrote:

Back when I was first starting to develop my UI Browser product, https://pfiddlesoft.com/uibrowser, there were three helpful Objective-C Cocoa code examples for NSBrowser from Apple: SimpleCocoaBrowser, ComplexBrowser, and AnimatedTableView. I haven't looked at them in a long time, but I'm sure you can find them somewhere.
I found the SimpleCocoaBrowser (thanks very much for your hint) and it works just fine.

The BrowserDelegate (called AppController in the sample code) implements 4 required + 1 optional methods:
- (id)rootItemForBrowser:(NSBrowser *)browser (optional)
- (NSInteger)browser:(NSBrowser *)browser numberOfChildrenOfItem:(id)item
- (id)browser:(NSBrowser *)browser child:(NSInteger)index ofItem:(id)item
- (BOOL)browser:(NSBrowser *)browser isLeafItem:(id)item
- (id)browser:(NSBrowser *)browser objectValueForItem:(id)item

Then I rewrote the BrowserDelegate in Swift, and again I got:

*** Illegal NSBrowser delegate (<SimpleBrowser.SwiftBrowserDelegate: 0x600001221260>). Must implement browser:willDisplayCell:atRow:column: and either browser:numberOfRowsInColumn: or browser:createRowsForColumn:inMatrix:

The reason:

I implemented:
func isLeafItem(_ item: Any?) -> Bool { false } ← wrong

instead of:
func browser(_ browser: NSBrowser, isLeafItem item: Any?) -> Bool { false }

A rather stupid mistake.
But the error message was not very helpful.

Thanks again for your help!

Gerriet.


Bill Cheeseman
 

Back when I was first starting to develop my UI Browser product, https://pfiddlesoft.com/uibrowser, there were three helpful Objective-C Cocoa code examples for NSBrowser from Apple: SimpleCocoaBrowser, ComplexBrowser, and AnimatedTableView. I haven't looked at them in a long time, but I'm sure you can find them somewhere.

UI Browser implements six NSBrowserDelegate methods that I describe in my source code comments as "required NSBrowser item-based delegate methods." They are: -rootItemForBrowser:, -browser:numberOfChildrenOfItem:, -browser:child:ofItem:, -browser:isLeafItem:, -browser:willDisplayCell:atRow:column:, and -browser:objectValueForItem:. You left two of them out of your list of "required" item-based methods.

UI Browser also implements a number of optional NSBrowserDelegate methods, including: -browser:numberOfRowsInColumn:, -browser:shouldShowCellExpansionForRow:column:, and -browserDidScroll:.

Bill Cheeseman

On Jan 3, 2021, at 1:31 AM, Gerriet M. Denkmann <gerriet@...> wrote:

So: how do I create an "item based NSBrowser" with a delegate using "item data source methods” ?


On Jan 3, 2021, at 1:31 AM, Gerriet M. Denkmann <gerriet@...> wrote:

So: how do I create an "item based NSBrowser" with a delegate using "item data source methods” ?


Gerriet M. Denkmann
 

Xcode tells me that:
'matrix(inColumn:)' was deprecated in macOS 10.10: Use the item based NSBrowser instead

And when I use parentForItemsInColumn it crashes with:
parentForItemsInColumn: is not supported for browsers with matrix delegates.

NSBrowser.h says:
" Note: the matrix based NSBrowser is deprecated in Mac OS 10.10. New code should use the item based interface.”

These seem to be the "matrix based NSBrowser” methods:
- (NSInteger)browser:(NSBrowser *)sender numberOfRowsInColumn:(NSInteger)column;
- (void)browser:(NSBrowser *)sender createRowsForColumn:(NSInteger)column inMatrix:(NSMatrix *)matrix;

"Alternatively, implement all of the following methods”
- (NSInteger)browser:(NSBrowser *)browser numberOfChildrenOfItem:(nullable id)item API_AVAILABLE(macos(10.6));
- (id)browser:(NSBrowser *)browser child:(NSInteger)index ofItem:(nullable id)item API_AVAILABLE(macos(10.6));
- (BOOL)browser:(NSBrowser *)browser isLeafItem:(nullable id)item API_AVAILABLE(macos(10.6));
- (nullable id)browser:(NSBrowser *)browser objectValueForItem:(nullable id)item API_AVAILABLE(macos(10.6));


So I tried:

func browser(_ browser: NSBrowser, numberOfChildrenOfItem item: Any?) -> Int { 5 }
func browser(_ browser: NSBrowser, child index: Int, ofItem item: Any?) -> Any { "test” }
func isLeafItem(_ item: Any?) -> Bool { false }
func browser(_ browser: NSBrowser, objectValueForItem item: Any?) -> Any? { item }

But when I run this, I get:

*** Illegal NSBrowser delegate (<TestBrowser.AppDelegate: 0x600003c19300>). Must implement browser:willDisplayCell:atRow:column: and either browser:numberOfRowsInColumn: or browser:createRowsForColumn:inMatrix:

and my browser is completely empty.

So: how do I create an "item based NSBrowser" with a delegate using "item data source methods” ?

Gerriet.