Is there a better way to avoid the "Sign Comparing" warning when comparing NSIndexPath and NSArray count strings?

I sent "Signed Comparision" warnings (aka -Wsign-compare) for my iOS project in Xcode (unexpectedly, it was disabled by default). After that, a lot of warnings appeared:

/Users/michalciuba/projects/GlobeMobile/Classes/ACMailController.m:86:19: Comparison of integers of different signs: 'NSInteger' (aka 'long') and 'NSUInteger' (aka 'unsigned long')

They are usually called by comparing a property row NSIndexPaththat is equal NSIntegerto the value returned by the count method for NSArray, for example:

if(indexPath.row < [self.myDataArray count]) 

You can simply fix the warning by custom:

if(indexPath.row < (NSInteger)[self.myDataArray count]) 

However, this must be done if in every place where such values โ€‹โ€‹are compared. And they are compared in dozens of places. I wonder if there is a better and smarter way to solve this problem? I do not want to turn off this warning, because it can help prevent problems like unsigned integer underflow.

+4
source share
1 answer

You can implement a category that will do this for you:

@implementation NSArray (SignedCount)

- (NSInteger) signedCount
{
    NSInteger count = (NSInteger)[self count];
    return count;
}

@end
+1
source

Source: https://habr.com/ru/post/1526510/


All Articles