Aleksandar • Vacić

iOS bits and pieces

Always use isEqualToString for string comparisons

While working on my iPhone app Quickie, I encountered one of many examples why you must always check your code on the actual device.

Quickie uses Core Data for storage and in one particular place I was comparing the NSString variable to a NSString-typed property of my CoreData class, QuickieList. Like this:

1
if (QuickieList.listName != theListName)

listName is defined as NSString and theListName is obviously that as well. In this particular instance, both of those had the value of “test”.

In the iPhone Simulator (running on 10.6.2) this comparison returned false, but on the iPhone running 3.1.2 it returned true. When changed into:

1
if (![QuickieList.listName isEqualToString:theListName])

result was the same.

Never – I repeat – never assume that simulator testing will be fine, even for seemingly small things. It can bite you when you least expect it.