比较NSIndexPath的行和NSArray计数时,是否有更好的方法来避免“符号比较”警告?

Mic*_*uba 6 objective-c clang nsinteger nsuinteger ios

我在XCode中为我的iOS项目打开了“签名比较”(又名-Wsign-compare)警告(令人惊讶的是,默认情况下它处于关闭状态)。之后,出现了许多这样的警告:

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

它们通常通过比较造成row的财产NSIndexPathNSInteger由“计数”的方法的返回值NSArray,如下所示:

if(indexPath.row < [self.myDataArray count]) 
Run Code Online (Sandbox Code Playgroud)

可以通过强制转换来简单地解决警告:

if(indexPath.row < (NSInteger)[self.myDataArray count]) 
Run Code Online (Sandbox Code Playgroud)

但是,如果要比较每个地方的值,则必须这样做。他们正在数十个地方进行比较。我想知道是否有更好,更聪明的方法来解决这个问题?我不想关闭此警告,因为它可能有助于防止出现无符号整数下溢之类的问题。

Rom*_*om. 2

您可以实现一个为您做这件事的类别:

@implementation NSArray (SignedCount)

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

@end
Run Code Online (Sandbox Code Playgroud)