比较NSInteger和NSUInteger

时间:2018-07-05 13:52:53

标签: objective-c nsarray nsindexpath nsinteger nsuinteger

我需要比较两个变量,以检查数组中是否存在索引: indexPath.row是NSInteger [self arrayOfData].count是NSUInteger

问题在于它们的类型不同,并且indexPath.row =-1时,它会自动转换为18446744073709551615(无符号长),这是我要避免的事情。

如何检查NSIndexPath的行定义的索引在NSArray中是否存在?

代码:

- (void) checkIfIndexExists:(NSIndexPath*) indexPath inArray:(NSArray*)arrayOfData {
    if (indexPath.row >= [self arrayOfData].count) {  // indexPath.row=-1 gets interpretted as 18446744073709551615 (unsigned long)
        DDLogDebug(@"Warning: Index doesn't exist {%@}", indexPath);
        return;
    }
}

1 个答案:

答案 0 :(得分:0)

这是我通过将unsigned long强制转换为long来解决问题的方法

- (void) checkIfIndexExists:(NSIndexPath*) indexPath inArray:(NSArray*)arrayOfData {
    if (indexPath.row >= (long) [self arrayOfData].count) {  // indexPath.row=-1 gets interpretted as 18446744073709551615 (unsigned long)
        DDLogDebug(@"Warning: Index doesn't exist {%@}", indexPath);
        return;
    }
}