Re: case-insensitive NSDictionary
Re: case-insensitive NSDictionary
- Subject: Re: case-insensitive NSDictionary
- From: Roland King <email@hidden>
- Date: Tue, 17 Mar 2009 09:14:10 +0800
That I didn't know - thanks, I'll make sure and combine the values from
repeated keys with commas.
Thanks also for all the other suggestions. Given this latest point,
subclassing NSMutableDictionary seems to be a good way to go as I can
adjust addObjectForKey to deal with the repeated key case.
The documentation for NSDictionary/NSMutableDictionary mentions they are
class clusters but doesn't go into the same depth as the NSString
documentation telling you what you need to do to subclass them. I found
the section about subclassing Class Clusters in the CocoaFundamentals
however and I can see what I have to do now, be a subclass of the
abstract class, declare my own storage and implement the primitives.
Thanks for all the suggestions and discussion.
Jeremy Pereira wrote:
When I had to solve the exact same problem, I created a new class
that wrapped the NSString as an ivar and defined my own hash and
isEqual methods (which both used the corresponding methods on a lower
case version of the string) and implemented NSCopying (easy because
my class was immutable) and used it for the keys. It was about 20
lines of code.
One slight gotcha you might need to be aware of is that headers need
not be unique if the value is a comma separated list i.e. the list
might be split over several headers with the same key. From section
4.2 of RFC 2616:
"Multiple message-header fields with the same field-name MAY be
present in a message if and only if the entire field-value for that
header field is defined as a comma-separated list. [i.e.,
#(values)]. It MUST be possible to combine the multiple header fields
into one "field-name: field-value" pair, without changing the
semantics of the message, by appending each subsequent field-value to
the first, each separated by a comma."
So either you write code to append the value strings from the header
to the values in your dictionary or your values in the dictionary
must be NSMutableArrays of strings.
On 16 Mar 2009, at 14:02, Roland King wrote:
Any good ideas for doing a key-case-insensitive NSDictionary of
NSString to NSString? I have some HTTP headers I want to stick in a
dictionary and look up later. HTTP headers have case-insensitive keys.
I could upper, or lowercase the key before putting it in the
NSMutableDictionary, but that means I lose the original case of the
key (I probably shouldn't care but I hate throwing away
information), and any user of my class has to know to upper or
lowercase the key before looking it up, or else I don't expose the
dictionary at all and just give lookup methods which hide the
details. That's a shame, I wanted the user to have access too all
the keys if they wanted it and wanted to just make the NSDictionary
available as a property.
I could make a case-insensitive string, but subclassing NSString
wasn't in my plans for Monday evening, or Tuesday and the user still
has to make the same NSCaseInsensitiveString() object to look up the
key.
I could make a case-insensitive version of NSMutableDictionary which
... oh that's worse, forget I even said that.
I think I'm going to lower-case on insert and tell the user the
NSDictionary of headers has lower-case keys but .. is there
something obvious I missed?
_______________________________________________
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
_______________________________________________
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