Chronor
Chronor

Reputation: 11

Cocoa: Binding to an array of NSStrings which are stored in the application's preferences

OK, I'm feeling really REALLY stupid for asking this question, but I've managed to stump myself so hard that I doubt I have the proper detachment to figure out the answer for myself any more. My apologies in advance...

I have been playing around with bindings for a while now and LOVE them. That and the KVO/KVC conventions built into Objective-C 2.0 have allowed me to dramatically slim down my applications while increasing their reliability, flexibility, etc. I say this for no other reason than to illustrate that I am at least moderately comfortable with bindings in general. That being said, my problem is this:

I have an application where I'd like to store an editable set of keywords by way of the preferences controller. Easy, right? Unfortunately, for the life of me I can't figure out how to do it. I set up an array controller and point it to the shared preferences object and give it a keypath for the array. The problem is that the objects being stored in the array are NSStrings and I don't see how NSStrings can be KVC-compliant for this kind of operation due to the lack of any "stringValue/setStringValue" methods. Without those, what keypath do I use in the NSTableView I have set up to facilitate manipulation of that list?

Also, since the shared preferences object is not one that I own, I'm not sure how to go about setting up a new array to represent an unedited/newly installed state. (Registering them as defaults doesn't seem to be working for me.)

So to reiterate my questions more succinctly: How can you bind a table view to an array of NSStrings and how can you store said array in an application's preferences?

Any pointers or advice that you can offer would be VERY much appreciated. The headaches I'm getting from this are starting to get the better of me. :)

Upvotes: 0

Views: 1582

Answers (2)

cocoafan
cocoafan

Reputation: 4894

I know this question is pretty old but I had a similar problem and landed to this page. I wanted to contribute with my solution. What I need is not a big list of strings. In fact a table view looked very ugly for my needs. I then remembered another control, NSTokenField. It binds to an array of string out of the box and looks even better for small list of short strings :)

Upvotes: 0

Peter Hosey
Peter Hosey

Reputation: 96363

You don't. You hold an array of model objects, each with at least one property (that of the string), and you bind the table column through an array controller to this array, with the model key path set to that of the string property.

Assuming your app is not simply a list-of-strings editor, you probably have other things that you can move into this model class. You should do so.

Upvotes: 1

Related Questions