我需要比较两个变量,以检查数组中是否存在索引:
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;
}
}
答案 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;
}
}