首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >比较NSInteger和NSUInteger

比较NSInteger和NSUInteger
EN

Stack Overflow用户
提问于 2018-07-05 21:52:53
回答 1查看 261关注 0票数 0

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

问题是它们属于不同的类型,当indexPath.row=-1时,它会自动转换为18446744073709551615 (无符号长整型),这是我试图避免的。

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

代码:

代码语言:javascript
复制
- (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;
    }
}
EN

回答 1

Stack Overflow用户

发布于 2018-07-06 16:48:32

下面是我如何通过将unsigned long转换为long来解决该问题的方法

代码语言:javascript
复制
- (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;
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51193116

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档