0

I have a fairly large app (objective-c), I decided to turn on the "Sign Comparison" warnings. I now have almost 100 of them. Most of them are similar to this:

if (recentSearches.count > indexPath.row) {
    //something
}

and the errors are similar to this:

Comparison of integers of different signs: 'NSUInteger' (aka 'unsigned int') and 'NSInteger' (aka 'int')

what are some good strategies or tips on how to tackle them all? Thanks

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
Nathan
  • 139
  • 10

1 Answers1

0

Really, you want to go ahead and change the declarations so the types match when you compare. For example, a count variable should be unsigned. That's by far the safest. If you are recycling a variable so it's compared to other things that may be both signed or unsigned, change that! Only typecast when you have explicitly verified it's safe do to so. For example, if a signed int is > 0 you can cast it to unsigned int. The other direction is not safe unless you handle the range of values an unsigned int can hold that are too big for a signed int!

RobP
  • 9,144
  • 3
  • 20
  • 33