Search code examples
iosobjective-ctype-conversioncompiler-warningssigned

Can I safely supress integer signedness conversion compiler warnings for "common" projects?


I have a legacy project that was working fine in iOS 5, 6, and 7. Not sure which Xcode started it, but I'm seeing a lot of "comparison of integers of different signs int and NSUInteger (aka unsigned int)" warnings.

Code like the below causes such warnings:

 for (int i = 0; i < names.count ; i++) // warning here
    {
        [self.images addObject:[NSNull null]];
    }
 for (NSString* name in names)
    {
        int index = [names indexOfObject:name];// warning here
    }

I completely understand the implication of this warning if I'm doing some science project or am building a calculator and am working with truly large integers, to the point where signedness would start to matter.

I'm looking at ~400 warnings in a project, of which good 200 are about sign conversion. I personally don't want to go back and add 200 different casts, as it's fugly and makes the code harder to read.

But for common cases: table view cell indexes, loops, array counts of less than 1000, etc - can I suppress such warnings and be okay? Am I likely to stumble on some obscure sign conversion bug in a "typical", non-scientific, non-math-heavy iOS project?


Solution

  • The better answer is to use the correct data type. Do not use int when working with collection counts and indexes. Look at the documentation. See that NSArray count is an NSUInteger. NSArray indexOfObject: is also NSUInteger.

    The simple solution is to change your int variables to the matching type of NSUInteger.

    Doing this avoids all kinds of possible issues and warnings and makes your code more correct.

    for (NSUInteger i = 0; i < names.count ; i++)
    {
        [self.images addObject:[NSNull null]];
    }
    
    for (NSString* name in names)
    {
        NSUInteger index = [names indexOfObject:name];
    }