0

I am trying to use NSManagedObjects with an NSTreeController and NSOutlineView in my macOS app.

I have a two level data in the outline view: Project and Item, both are NSManagedObjects set up in my model.

In my storyboard, I added this:

enter image description here

enter image description here

And I added this in my code:

extension Project {
    @objc var isLeaf: Bool {
        return false
    }
    
    @objc var childCount: Int {
        return items?.count ?? 0
    }
    
    @objc var children: [Item] {
        return items?.allObjects as! [Item]
    }
}

extension Item {
    @objc var isLeaf: Bool {
        return true
    }
    
    @objc var childCount: Int {
        return 0
    }
    
    @objc var children: [NSManagedObject] {
        return []
    }
}

When I run the app, I get the following error:

Thread 1: "[<MyApp.ProjectBrowserViewController 0x600001580820> valueForUndefinedKey:]: this class is not key value coding-compliant for the key isLeaf."

It doesn't say which class but that error shows up twice, so I am guessing once for Project, and one for Item.

What am I missing here, how do I fix this? Will gladly add more info if needed.

koen
  • 5,383
  • 7
  • 50
  • 89
  • 1
    That error message says you are trying to use `isLeaf` on an instance of `ProjectBrowserViewController`. Check your tree view configuration, you seem to be asking for values on the wrong thing. – Tom Harrington Nov 07 '21 at 22:17
  • 1
    What is bound to what? Tip: Add `-NSBindingDebugLogLevel 1` to the “Arguments to be passed at launch” list. – Willeke Nov 07 '21 at 22:18
  • Thank you both, I will need to do some debugging. In the meantime, are the settings in the screenshots and the code in my question correct? – koen Nov 07 '21 at 23:00
  • 1
    Note that the [documentation](https://developer.apple.com/documentation/swift/cocoa_design_patterns/using_key-value_observing_in_swift) say "Mark properties that you want to observe through key-value observing with both the `@objc` attribute **and** the `dynamic` modifier. " (emphasis mine) – Scott Thompson Nov 08 '21 at 02:53
  • Adding `dynamic` did not solve this. I'll keep digging. – koen Nov 08 '21 at 11:05
  • 1
    Any bindings in the Referenced Bindings section of the Connections Inspector of the `ProjectBrowserViewController`? What is the error with `-NSBindingDebugLogLevel 1`? – Willeke Nov 08 '21 at 12:02
  • @willeke Yes - I had a lingering connection for `content` to the Tree Controller. Removing that made the crash go away. The error did not change with the debug flag. – koen Nov 08 '21 at 22:21

0 Answers0