Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing signed and unsigned values in objective-c

Recently I have encountered a problem with comparing signed and unsigned values in objective-c. Here is an example of this problem:

NSArray *array = [NSArray array];
NSLog(@"Count = %d", array.count);
NSLog(@"Count - 2 = %d", array.count - 2);
if (array.count - 2 > 0) {
    NSLog(@"A");
} else {
    NSLog(@"B");
}

At the first glance it seems that the following code should print B. However, when you run it this is what you see:

Count = 0
Count - 2 = -2
A

Now I know that the problem is with comparing signed and unsigned values (array.count is unsigned long).

However, this kind of error is very hard to spot (the code compiles, there is no warning and you may not notice that array.count is unsigned and that it matters). The question is how could I avoid such situations? Is there a safe way of comparing signed and unsigned values?

It is also interesting why NSLog(@"%d", array.count - 2) prints -2?

like image 816
Piotr Avatar asked Oct 10 '12 08:10

Piotr


1 Answers

There is a compiler option, -Wsign-conversion, that warns you about potential signedness problems. In Xcode, you can find it under Implicit Signedness Conversion in the Build Settings.

like image 61
Guillaume Avatar answered Sep 30 '22 09:09

Guillaume