Re: Getting the content-type in a HTTP response
Re: Getting the content-type in a HTTP response
- Subject: Re: Getting the content-type in a HTTP response
- From: Nir Soffer <email@hidden>
- Date: Sun, 27 Jan 2008 00:44:27 +0200
On Jan 26, 2008, at 01:57, Dave Carrigan wrote:
So my next question is if it's possible to override NSDictionary's
comparison operator to be case insensitive (a la STL's maps) or if
I should just create a new dictionary that copies the old
dictionary with downcased keys. The former would be more
bulletproof since it wouldn't require other programmers to remember
to downcase all their dictionary lookups.
NSDictionary uses -isEqual: - you probably can't change it. You can
create a CFMutableDictionaryRef with CFDictionaryKeyCallBacks that
will do case insensitive compare, then copy the original keys and
values to this dictionary and use it for accessing the values.
It can look like this:
static Boolean caseInsensitiveCompare (const void *a, const void *b);
static CFHashCode caseInsensitiveHash (const void *value);
@implementation NSHTTPURLResponse (XYZAdditions)
- (NSDictionary *)xyzHTTPHeaders;
{
NSDictionary *src = [self allHeadersFields];
CFDictionaryKeyCallBacks keyCallbacks;
keyCallbacks.retain = CFRetain;
keyCallbacks.release = CFRelease;
keyCallbacks.copyDescription = CFCopyDescription;
keyCallbacks.equal = caseInsensitiveCompare;
keyCallbacks.hash = caseInsensitiveHash;
CFMutableDictionaryRef dest = CFDictionaryCreateMutable (
kCFAllocatorDefault,
[src count], // capacity
&keyCallbacks,
&kCFTypeDictionaryValueCallBacks
);
// Now copy keys and values from src to dest
return (NSDictionary *)dest;
}
@end
static Boolean caseInsensitiveCompare (const void *a, const void *b);
{
// Assuming that keys are strings
return [(NSString *)a caseInsensitiveCompare:(NSString *)b] ==
NSOrderedSame;
}
static CFHashCode caseInsensitiveHash (const void *value);
{
// Assuming that keys are strings
return [[value lower] hash];
}
Compiles in Mail.app :-)
Example usage:
- (void)connection:(NSURLConnection*)connection didReceiveResponse:
(NSURLResponse*)theResponse
{
NSString* content_type = [[(NSHTTPURLResponse*)theResponse
xyzHTTPHeaders] objectForKey:@"coNtent-tyPe"];
}
Best Regards,
Nir Soffer
_______________________________________________
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