Re: Getting started with Cocoa Bindings in My Project?
Re: Getting started with Cocoa Bindings in My Project?
- Subject: Re: Getting started with Cocoa Bindings in My Project?
- From: Alex Hall <email@hidden>
- Date: Mon, 07 Sep 2015 08:25:52 -0400
> On Sep 7, 2015, at 02:07, Ken Thomases <email@hidden> wrote:
>
> On Sep 7, 2015, at 12:01 AM, Alex Hall <email@hidden> wrote:
>
>> My first step is to make a KVO-compliant backing store, because Swift arrays won't do the job. As you said, my goal of updating an array and having my table automagically update itself to match the array just won't happen. What I need is a class that implements the methods described in the link you provided,so that bindings will work correctly. Obviously I can use an array internally, but the interface of the class *must* offer the necessary methods. I'll call this new class MyKVOCClass, because I'm terrible at naming things.
>
> It's not particularly that the class must offer the necessary methods. It's that your code must use those methods to modify the array or KVO is not aware of the changes you make.
That makes sense. I'd be able to just have the model class expose what it needs to, handling all its internals by itself. There's no need for the controller to care which tab is selected or which array something gets added to, because the KVO methods will expose that. If I'm understanding correctly, I would turn my model itself into a KVO-compliant class which would, in the eyes of my array controller, have just one array.
> It sounds like you're suggesting making a class just to be an array-like thing that is KVO-compliant. That sounds to me like you're heading down the wrong road. The class which needs to be KVO-compliant is the thing which currently holds the arrays (even if a bit indirectly). That brings us to the next point…
>
>> Next, remember that my model is a dictionary of arrays right now. The model has a single variable that can change to point at any of the arrays in the dictionary; the table is to display only that selected array (the table is view-based, one column, no multi-selecting, no editing).
>
> Can you explain in some more concrete terms why this level of dynamism is required? What does the dictionary represent in some real-world sense or "business logic"? What do the various arrays represent? Is there a fixed set of arrays or is that variable? What makes one of the arrays current and what does that signify (other than that's the one shown in the table)? What other implications does it have? What do the arrays have in common? Why, other than happenstance or convenience, does it make sense for them to all be part of the same controller?
Sure. This is a Twitter app, which shows one type of tweet at a time. That is, the table can show one of home, mentions, DMs, favorites, RTs of the user, Twitter searches, tweets from a different user, etc. When an array is "active", right now it just shows its own tweets in the table, but I hope to do a bit more in the future, like changing the window title to show the tab's name. I'm just starting, so I know a lot of refactoring will have to happen; I'm in the "just get it working" stage.
My class is TweetDataController, and it currently does *everything* that isn't related to my view. It holds my dictionary of tabs, but it also calls the Twitter framework methods to download new tweets, it posts tweets, it parses them for display, it replaces short URLs with expanded ones in tweet text… I know it's not good to have all that logic in one place, and I was going to refactor it soon, but as I said, I'm still getting things working and seeing how the app--and its internals--will evolve and need to be changed.
Anyway, the idea behind the dictionary of arrays is that each tab (list of tweets) is an array of Tweet objects, and all the tabs are in the dictionary, accessed by String constants. For instance, when the user hits cmd-4 to show his favorites, the view controller calls TweetDataController.getTabByName(TwitterDataController.favoritesTabName). In the model, currentTab (a string) is set to the favoritesTabName string, and then the view controller reloads the table. Since the active tab has changed, reloading gets the array of tweets for favorites and my table shows favorites.
I'm not bent on using this setup, it just made sense at the time, and I needed something. This is why I said earlier that I'm using a dictionary, but only need an array controller. It's also why I need that computed property, so that the controller is always pointing at the correct array. I see what you're saying, though: TweetDataController can implement the KVO-specific methods and expose only what it needs to, no need to worry about the active tab. As I said above, the model would then appear to the controller to just be a KVO-ready array.
>
> I'm asking because this design strikes me as one which will cause trouble and I'm looking for handholds for a suggested refactoring. I'll wait for more info before getting into the other stuff.
>
> Regards,
> Ken
>
--
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