Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Getting the content-type in a HTTP response




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:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden
References: 
 >Getting the content-type in a HTTP response (From: Dave Carrigan <email@hidden>)
 >Re: Getting the content-type in a HTTP response (From: Bill Garrison <email@hidden>)
 >Re: Getting the content-type in a HTTP response (From: Dave Carrigan <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.