• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: case-insensitive strings
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: case-insensitive strings


  • Subject: Re: case-insensitive strings
  • From: Andy Lee <email@hidden>
  • Date: Thu, 10 Oct 2002 15:21:59 -0400

On 10/10/02 5:48 pm, Matt Neuburg <email@hidden> wrote:
> The idea that you can't flip case sensitivity on
> or off for an individual string instance is just nutty to me.

I wouldn't be comfortable having such a toggle for individual string instances -- or even for individual classes. If you have...

NSString *regularString = @"abc";
CaseTogglableString *togglingString =
[CaseTogglableString stringWithString:@"AbC" insensitive:YES];

...you could end up with...

[regularString isEqual:togglingString] == NO

...whereas:

[togglingString isEqual:regularString] == YES

This asymmetry between closely related classes would seem very weird to me.

Also, what if you were to use those two strings as dictionary *keys* rather than values? I'm not exactly sure what would happen, but I suspect you might get different results depending on the order in which you insert them -- which would definitely be nutty.

At 7:00 PM +0100 10/10/02, Chris Ridd wrote:
This is particularly barf-worthy since CoreFoundation has supported
case-insensitive compares since 10.0:

res = CFStringCompare(str1, str2, kCFCompareCaseInsensitive);

So has Foundation. See [NSString -caseInsensitiveCompare:].

It seems to me Matt's original issue was not whether you can perform case-insensitive comparisons at all, but how to force a dictionary's -allKeysForObject: method to do so. IMO the ideal solution would be to tell the dictionary to use a comparison method other than -isEqual:, or optionally a comparison function (the way you can do with NSArray's sorting methods). This isn't possible now, but it seems a reasonable thing to ask for via a bug report.

One option is a wrapper class such as Ali suggested. But personally, under the circumstances, I would probably not even try to use -allKeysForObject:. Instead, I'd write my own method that iterates through the dictionary entries, something like:

// completely untested code:

- (NSArray *)keysForCaseInsensitiveString:(NSString *)aString
inDictionary:(NSDictionary *)aDictionary
{
NSMutableArray *resultKeys = [NSMutableArray array];
NSEnumerator *keyEnum = [aDictionary keyEnumerator];
id key;

while ((key = [keyEnum nextObject]))
{
if ([key isKindOfClass:[NSString class]])
{
if ([key caseInsensitiveCompare:aString] == NSOrderedSame)
{
[resultKeys addObject:key];
}
}
}

return resultKeys; // convert to an immutable NSArray first if you like
}

This way you don't have to change a thing about what you're doing except to use this method instead of -allKeysForObject:.

The only reason I didn't write the above as a category method of NSDictionary (which would be more natural) is that NSDictionary is a class cluster. If there's some way to add a category to a class cluster, so much the better, but that's not a can of worms I've ever stuck my head into.

--Andy
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: case-insensitive strings
      • From: Ondra Cada <email@hidden>
    • Re: case-insensitive strings
      • From: Chris Ridd <email@hidden>
References: 
 >Re: case-insensitive strings (From: Chris Ridd <email@hidden>)

  • Prev by Date: Re: SMySQL Problems
  • Next by Date: is protected broken, or am I?
  • Previous by thread: Re: case-insensitive strings
  • Next by thread: Re: case-insensitive strings
  • Index(es):
    • Date
    • Thread