Getting started with Cocoa Bindings in My Project?
Getting started with Cocoa Bindings in My Project?
- Subject: Getting started with Cocoa Bindings in My Project?
- From: Alex Hall <email@hidden>
- Date: Sun, 06 Sep 2015 11:59:12 -0400
Hello list,
I've got a Mac app that has a table with a data source. Every few minutes, the data source updates itself, and when that happens, I call myTableView.reloadData(). This is a prime candidate for Cocoa bindings, so I thought I'd learn how that mechanism works, and make my code much prettier at the same time. It hasn't worked so far.
I understand the theory, I think. Where I'm getting lost is adapting what I've read to my own app. My data model is a dictionary of arrays, and the table is showing only one array at a time. I'm *not* trying to map the whole dictionary to the table at once; the model has a pointer that selects the current array in use, and then works on *only* that array. Methods in my view controller (specifically, menu items) change that pointer, letting the user move from array to array. The table, again, only ever shows one array at a time. It is a single-column table, not editable, not multi-selectable.
Since I'm using arrays, albeit retrieved from a dictionary, I thought an NSArrayController would do the job. I'm not sure what to enter for the key path, though. My view controller has a reference to both my table and my data model object, so I thought I'd give it the current array. I tried making the key path dataModel.myDict[datamodel.currentArray], but Xcode informed me that "myDict[dataModel" is not a valid key. Why it pulled just that part of what I'd actually entered, I'm not sure.
I then made a computed property in my view controller, so I could give the array controller a single-level property name but still get at the desired array. That failed too, but with slightly different error messages. This time, I saw
[Swift._SwiftDeferredNSArray persistentStoreCoordinator]: unrecognized selector sent to instance 0x60800003ddc0
and then "an uncaught exception was raised", and then the first error again, for the same object.
I'm thoroughly confused now, and I'm not really sure where to start searching for answers, because I don't think I fully understand the problems I'm having. Now that I've rambled on about the background, I'll try to summarize what I'm having the most trouble with, conceptually and practically:
* I'm using Swift. Will that be a problem? I made both the computed property in my view controller and the dictionary of lists in my model "dynamic", and I made the object class of which my array's contents are instances subclass NSObject. Is there anything else I should do or know specific to Swift?
* MVC I get, but why are my binding choices for my NSArrayController only controllers themselves? That is, why do I need to hook up my controller to my model *through another controller*? Should I not just give the NSArrayController my model directly?
* When binding my NSTableView to my array controller, what, exactly, do I use for keys for both the table as a whole and the table cell? Again, I only have one column, if that matters.
* When and if I get all this working, do i still need my data source/delegate? It sounds like bindings replace that, but if so, where do my table cells get generated? Right now I use that function to set the textField.stringValue of the cell to the correct item in the current array; where would that happen in a bindings context?
* Assuming for a moment that my key is correct, and I want to bind to the array currently in use by the model, how would I do that? A dictionary lookup returns an optional, so for the moment--just to get things going--I'm forcing it to unwrap. Ideally, though, I'd include some way of handling a nil. How do bindings work with optionals?
At the end of the day, I want what bindings promises: my model updates, and each time it does (a row changes, is added, or is removed) my table updates to reflect that change. I'm using reloadData(), but I thought bindings would be far more elegant, extensible, and simple. I think it will be once I really understand how to use this system, but until then, it's seeming like way more trouble than it's worth. I really want to grasp this concept, though, and I'm still researching and reading. I'm hoping that being able to ask specific questions about my specific model will help to clarify what general-purpose explanations haven't. Thanks in advance for your help and patience.
--
Have a great day,
Alex Hall
email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden