[NSCharacterSet lowercaseLetterCharacterSet] is to be AVOIDED

My unit tests have been failing and I’ve been trying to figure the issue out for about 7 hours now. It turns out it is

[NSCharacterSet lowercaseLetterCharacterSet]

You would think this will return a character set that only contains the lower-case letters in the alphabet. But NO NO NO NO, that’s not what this method does. Apple says:

Informally, this set is the set of all characters used as lowercase letters in alphabets that make case distinctions.

So I wrote a method that prints out all the characters inside an NSCharacterSet and passed the characters in lowercaseLetterCharacterSet to this method and guess what characters I got? Here is the screenshot of my console output.

To solve this issue, avoid using that method. Instead, do this:

[NSCharacterSet characterSetWithCharactersInString:@”abcdefghijklmnopqrstuvwxyz”]

I hope this will help you save hours of head scratching.

Leave a comment