FROM : Nir Soffer
DATE : Sat Jan 26 23:44:27 2008
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
DATE : Sat Jan 26 23:44:27 2008
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
| Related mails | Author | Date |
|---|---|---|
| Dave Carrigan | Jan 25, 23:44 | |
| Kyle Sluder | Jan 26, 00:22 | |
| Bill Garrison | Jan 26, 00:35 | |
| Dave Carrigan | Jan 26, 00:57 | |
| John Joyce | Jan 26, 01:49 | |
| Kyle Sluder | Jan 26, 02:05 | |
| Andy Lee | Jan 26, 02:29 | |
| Nir Soffer | Jan 26, 23:44 | |
| Nir Soffer | Jan 27, 01:54 | |
| Dave Carrigan | Jan 28, 18:11 |






Cocoa mail archive

