Re: Can you "bind" two values into one?
Re: Can you "bind" two values into one?
- Subject: Re: Can you "bind" two values into one?
- From: Jerry Krinock <email@hidden>
- Date: Thu, 24 May 2007 19:09:52 -0700
On 2007 May, 23, at 9:55, mmalc Crawford wrote:
On May 23, 2007, at 8:44 AM, Jerry Krinock wrote:
For the last hour, I've been trying to figure out how to bind two
table columns such as this to a single string in storage,
bidirectionally, using Cocoa bindings.
Bidirectionally you can't (in the read-only direction you can use
pattern values).
But I keep running into a wall, trying to transform two objects
into one. What's the smart way to do this?
In your model class, implement accessors for fullName.
-fullName returns the appropriate concatenation of firstName and
lastName.
-setFullName: parses the input parameter into firstName and
lastName and sets their values.
Well, mmalc, first you said I can't, but then you kind of told me how
to do it, so I did.
So I bound the NSArrayController to my NSUserDefaultsController
(actually the data is stored in user defaults) using a reversible
transformer which I made. It transforms an NSArray of strings into
another NSArray of equivalent MyClass objects. My MyClass class has
accessors for firstName, lastName and fullName as you describe, and
implemented an +initialize method which sets fullName as dependent on
the other two keys, as suggested by Savant. I set the "Object Class
Name" attribute of the NSArrayController to MyClass.
It only worked in the plist-to-UI direction until, after several
hours of guessing, I went into Interface Builder > array controller >
Bindings > Content Array, and checked "Handles Content as Compound
Value". Now it works in both directions: adding, modifying or
removing entries.
Thanks all for the help.
Jerry
On 2007 May, 23, at 10:14, I. Savant wrote:
... in general, I suggest going back to this marketing hack and
diplomatically requesting clarification on the ultimate goal, not how
I.S., I appreciate your feelings about marketing, but please note the
words "Say..." in my original post. "Say... means that they
situation is hypothetical. Indeed, I was oversimplifying the actual
case in order to tell an attention-getting story that would be
universally understood. In hindsight, it looks like I went "over the
top"; my story was too close to peoples' nerves and thus too
effective. Sorry about that. In fact, my actual application is not
dealing with first and last names, but the problem is the same. In
case anyone is interested in the real problem behind the story, here
it is...
My users can maintain a list of phrases in user defaults. But it
makes a difference whether or not a given phrase ends in a trailing
space. Sometimes a user may need a trailing space, sometimes not.
Since trailing spaces are invisible on the screen, this could cause
massive user frustration. So, I ignore any trailing space and have
in each row a second column with a checkbox to say yes/no trailing
space, with a detailed explanation of its effect. Now, it is true
that I could carry the hasTrailingSpace property into the user
defaults, and modify some other (sorting) code in the app to read,
instead of a string, this 2-entry dictionary, and from it reconstruct
the string + space. But I view this as a kludge. Besides making the
sorting less efficient, the string is logically the data of
interest. The trailing-space conundrum is simply an artifact of the
UI, so I believe it should be resolved as close to the UI as possible.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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