Re: NSDictionary & KVC problem
Re: NSDictionary & KVC problem
- Subject: Re: NSDictionary & KVC problem
- From: Geoff Hopson <email@hidden>
- Date: Mon, 25 Oct 2004 14:23:05 +0100
Is that empty string in allValues causing problems?
Geoff
On Mon, 2004-10-25 at 14:12, Nathan Dumar wrote:
> Art and Pierre,
>
> Sorry, I should have explained better. thisUser.preferences() returns
> an array of Preference objects, each of which has a "name" and a
> "value" field (both Strings). So
> thisUser.preferences().valueForKey("value") should return an array of
> Strings (right?), one for each of the objects (of which there are 6).
> The same for the value attribute.
>
> The results were identical when I used:
>
> prefs = new NSDictionary(new
> NSArray(thisUser.preferences().valueForKey("value")), new
> NSArray(thisUser.preferences().valueForKey("name")));
>
> or
>
> NSArray values = new
> NSArray(thisUser.preferences().valueForKey("value"));
> NSArray names = new NSArray(thisUser.preferences().valueForKey("name"));
> prefs = new NSDictionary(values, names);
>
> For some reason I end up with (I think) an NSDictionary with one array
> key and one array value instead of 6 string keys and 6 string values.
> The docs say that the constructor is supposed to go one-by-one through
> each array, adding each item to the dictionary.
>
> valueForKey() and objectForKey() return null for keys that I know are
> (should be?) in there. prefs.objectForKey("uF1") returns null.
> prefs.valueForKey("uF1") returns null. They should return "Field 1".
> For convenience, here's the allKeys() and allValues() output again:
>
> prefs.allKeys() yields (("idNum", "uF1", "archiveTo", "uF2", "uF3",
> "histLimit"))
>
> prefs.allValues() yields (("numOnly", "Field 1", "", "Field 2", "Field
> 3", "8"))
>
>
> Being new, I know I'm doing something wrong that should have been
> obvious, and I'm going to be embarrassed when I finally figure out what
> it is.
>
> // I should just make a loop to step through each item. Save us all
> the trouble.
>
> Thanks for your help guys.
>
> Take care,
> Nathan
>
> On Oct 24, 2004, at 6:52 PM, Art Isbell wrote:
>
> > I would check the array returned by
> > thisUser.preferences().valueForKey("value") to determine whether it's
> > an array with a single array element. If preference.value() is an
> > array, then thisUser.preferences().valueForKey("value") will return an
> > array of arrays. Seems unlikely from the names of your keys, but
> > something like this must be occurring.
> >
> > Aloha,
> > Art
>
> On Oct 25, 2004, at 3:17 AM, email@hidden wrote:
>
> > You forgot to cast the retun value of valueForKey() into an NSArray.
> > You are thus using the NSDictionary(Object, Object) constructor. Java
> > is that stupid.
> >
> > All the same you are using the NSArray(Object) constructor rather than
> > the NSArry(NSArray) constructor.
> >
> > Pierre
> >
> > -----Original Message-----
> > From: webobjects-dev-bounces+pierre.bernard=email@hidden
> > [mailto:webobjects-dev-bounces+pierre.bernard=email@hidden]On
> > Behalf Of Nathan Dumar
> > Sent: Monday, October 25, 2004 12:43 AM
> > To: Ben Ketteridge
> > Cc: WebObjects List
> > Subject: Re: NSDictionary & KVC problem
> >
> >
> > Ah hah! I think you're right, that it's making a dictionary of one key
> > and one value, each an array of the keys and values. I was following
> > the docs:
> >
> > public NSDictionary(NSArray objects,
> > NSArray keys)
> > Creates an NSDictionary with entries from the contents of the keys and
> > objects NSArrays. This method steps through objects and keys, creating
> > entries in the new dictionary as it goes. Each key object and its
> > corresponding value object is added directly to the dictionary.
> >
> > So why did it not do that?
> >
> > Here's my code:
> >
> > prefs = new NSDictionary(thisUser.preferences().valueForKey("value"),
> > thisUser.preferences().valueForKey("name"));
> >
> > or equally (I tried both):
> >
> > NSArray values = new
> > NSArray(thisUser.preferences().valueForKey("value"));
> > NSArray names = new
> > NSArray(thisUser.preferences().valueForKey("name"));
> > prefs = new NSDictionary(values, names);
> >
> > Thanks,
> > Nathan
> >
> >
> > On Oct 24, 2004, at 5:13 AM, Ben Ketteridge wrote:
> >
> >> On Sat, 23 Oct 2004 20:27:11 -0400, Nathan Dumar
> >> <email@hidden> wrote:
> >>> I have an NSDictionary called "prefs." I've loaded it with pairs of
> >>> strings, as follows:
> >>>
> >>> prefs.allKeys() yields (("idNum", "uF1", "archiveTo", "uF2", "uF3",
> >>> "histLimit"))
> >>>
> >>> prefs.allValues() yields (("numOnly", "Field 1", "", "Field 2",
> >>> "Field
> >>> 3", "8"))
> >>>
> >>> So, when I do prefs.valueForKey("uF1") I should get "Field 1", but I
> >>> don't. I get null. In fact, I get null for any of the keys. When
> >>> bound to a WOString (value = prefs.uF1) it's the same problem. What
> >>> >> am
> >>> I doing wrong?
> >>
> >> Are the (( an artifact of the way you've presented the data in your
> >> email, or are they part of what toString outputs?
> >>
> >> If the former, I don't know what you've done wrong, but if it's the
> >> latter, it would appear there is only one key in the dictionary, and
> >> that is the array ("idNum", "uF1", "archiveTo", "uF2", "uF3",
> >> "histLimit") and there is one value in the dictionary, and that is the
> >> array ("numOnly", "Field 1", "", "Field 2", "Field 3", "8").
> >>
> >> Creating the NSDictionary can be done like this:
> >> NSDictionary dict = new NSDictionary(new Object[] {
> >> "idNum", "uF1", "archiveTo", "uF2", "uF3", "histLimit"
> >> }, new Object[] {
> >> "numOnly", "Field 1", "", "Field 2", "Field 3", "8"
> >> });
> >>
> >> try this and see if dict.valueForKey("uF1") still gives null.
> >>
> >> HTH,
> >> Ben
> > _______________________________________________
> > Do not post admin requests to the list. They will be ignored.
> > Webobjects-dev mailing list (email@hidden)
> > Help/Unsubscribe/Update your Subscription:
> > email@hidden
> >
> > This email sent to email@hidden
> >
> >
> >
> >
> > **********************************************************************
> > This email and any files transmitted with it are intended solely for
> > the use of the individual or entity to whom they are addressed.
> > If you have received this email in error please notify the sender
> > of this message. (email@hidden)
> > This email message has been checked for the presence of computer
> > viruses; however this protection does not ensure this message is
> > virus free.
> > Banque centrale du Luxembourg; Tel ++352-4774-1; http://www.bcl.lu
> > **********************************************************************
> >
>
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Webobjects-dev mailing list (email@hidden)
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden