[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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s