I am trying to work with the examples on your book. For this chapter, your example does not work with the storyboards, but when I set the project, it starts with one already. Then, I do not know how to “add” the view and data to fit and run properly in order for me to see the results. Please advise me how to proceed. Should I follow an alternate way to start the example? I am finding very frustrating to follow the book along with the new Xcode version…
Thanks for your help on this!!!
luis
I had a problem with NSArrayController in this chapter also, and I was very frustrating but I succeeded in the end: (Swift 3, Xcode 8.31).
- Start the project with defaults (AppDelegatee.swift, ViewController.swift, Document.swift, Main.storyboard)
- Add an Employee class as described in the book
- Add employees in Document as described in the book
- do not touch (for now) AppDelegate and ViewController files
- In Main.storyboard find ArrayController in Object Library and add it to the View Controller Scene in outline
- In ArrayController’s Attributes Inspector find Class Name and enter RaiseMan.Employee
- In ArrayController’s Bindings Inspector find ContentArray, bind to View Controller and in Model Key Path enter representedObject.employees
- In Document.swift add in the end of the func MakeWindowControllers, after self.addWindowController(windowController: windowController.contentViewController!.representedObject = windowController.document
for me it works perfectly
I tried the same as you, setting representedObject
of the ViewController
to the Document
instance:
let viewController = windowController.contentViewController as! ViewController
viewController.representedObject = windowController.document
This almost works, the only problem is that, while using the RaiseMan app, if I close a document window, I get the following error on the debugger:
2017-04-26 19:12:14.828877-0300 RaiseMan[68170:1704702] [General] Cannot remove an observer <NSAutounbinderObservance 0x608000023180> for the key path "representedObject.employees" from <RaiseMan.ViewController 0x6080000c34f0>, most likely because the value for the key "representedObject" has changed without an appropriate KVO notification being sent. Check the KVO-compliance of the RaiseMan.ViewController class.
I’m assuming we shouldn’t set the representedObject to the Document
, but should instead set it to a model object like suggested in this Apple Q&A link: https://developer.apple.com/library/content/qa/qa1871/_index.html
Thinking of that, I also tried the following:
viewController.representedObject = employees
That works (also updating the ArrayController binding to just representedObject
instead of representedObject.employees
): I don’t get the debugger error and everything seems cool. However, it breaks down in chapter 11 because now I can’t override insertValue()
, removeValue()
and such which are necessary for undo.
So I wonder, does anyone have a definitive solution for this?
So… the plot thickens…
I just realized this bit of code in the ViewController
:
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
This is template code that’s generated by creating the Xcode project. My guess is this override is breaking KVO for the representedObject
key, so I took it out… and guess what, the error went away!
So I think I can move on now. Hope this helps someone having the same issues.
Sigrist
Can you post your code?
Thanks,