简体   繁体   English

比较NSInteger和NSUInteger

[英]Comparing NSInteger and NSUInteger

I have two variables that I need to compare, in order to check if an index exists in an array: indexPath.row is NSInteger [self arrayOfData].count is NSUInteger 我需要比较两个变量,以检查数组中是否存在索引: indexPath.row是NSInteger [self arrayOfData].count indexPath.row是NSUInteger

The problem is that they are of different types and when indexPath.row =-1 it gets automatically converted to 18446744073709551615 (unsigned long), which is what I'm trying to avoid. 问题是它们的类型不同,当indexPath.row = -1时,它会自动转换为18446744073709551615(无符号长),这是我要避免的。

How do I check if an index defined by NSIndexPath 's row exists in an NSArray ? 如何检查由NSIndexPath的行定义的索引在NSArray是否存在?

Code: 码:

- (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;
    }
}

Here is how I solved the problem by casting unsigned long to long 这是我如何铸造解决了这个问题unsigned longlong

- (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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM