警告:由于数据类型的范围有限,比较总是正确的,这会导致崩溃 [英] warning: comparison is always true due to limited range of data type causes crash

查看:273
本文介绍了警告:由于数据类型的范围有限,比较总是正确的,这会导致崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个警告,我找不到原因.

I have a warning that I am unable to find the cause of.

我正在遵循Cocoa编程中的文本中的指令代码,该代码实现了基于文档的图像幻灯片放映.此警告使while循环执行的次数超过正确的次数,从而导致程序崩溃.从作者的网站下载的代码没有此问题.

I am following instructional code in a text on Cocoa programming that implements a document based image slide show. This warning causes a while loop to be executed more than the correct number of times which causes the program to crash. The code downloaded from the author's website does not have this problem.

在我的版本中,我认为这只是一个错字问题,但仔细阅读两个版本的代码后,我发现没有任何区别.然后,我用作者的版本系统地替换了我版本中的每个.h,.m,.xib和其他资源文件,并清理了所有目标并在每次替换后重新构建.

I assumed it was a simple matter of a typo in my version but carefully reading both versions of code I was unable to come across any differences. I then systematically replaced every .h, .m, .xib and other resource file in my version with the author's version cleaning all targets and rebuilding after each replacement.

但是,直到我最终用作者的版本替换.xcodeproj文件后,警告才会消失.此时,警告将清除,并且代码将运行而不会崩溃.然后,我尝试了另一种方法,即用我的.h和.m文件一次替换了作者版本中的每个.h和.m文件,清理了所有目标,构建并再次没有警告或崩溃.我认为这可能是.plist文件中的某些设置,但是交换该文件的两个版本似乎没有任何效果.我似乎可以将其范围缩小到.xcodeproj捆绑包中的project.pbxproj文件,但是我看不到那里列出的任何构建设置如何导致此问题.

However the warning does not go away until I finally replace the .xcodeproj file with the author's version. At which point the warning clears and the code runs without crashing. I then experimented the other way by replacing each of the .h and .m files in the author's version with my .h and .m files all at once, cleaned all targets, built and again no warnings or crashes. I thought it might be some setting in the .plist file but swapping the two versions of that file seem to have no effect. I seem to be able to narrow it down to the project.pbxproj file in the .xcodeproj bundle but I can't see how any of the build settings listed there could cause the problem.

如果有人可以提供对该问题的任何见解或可以推荐一种调试方法,我将不胜感激.警告和带有while循环的相关代码段如下:

I would appreciate it if anyone could offer any insight to the problem or can recommend a method to debug it. The warning and the related code segment with the while loop are as follows:

构建警告:

SlideShowDocument.m: In function '-[SlideShowDocument removeSlidesAtIndexes:]':
SlideShowDocument.m:191: warning: comparison is always true due to limited range of data type

调试器控制台输出:

Slide Master[665:a0f] HIToolbox: ignoring exception '*** -[NSCFArray objectAtIndex:]: index (4294967295) beyond bounds (3)' that raised inside

代码:

- (void)removeSlidesAtIndexes:(NSIndexSet*)indexes;
{
    NSMutableArray *slideList = [NSMutableArray array];

    unsigned int index = [indexes firstIndex];

    while (index != NSNotFound) {
        Slide *slide = [mSlides objectAtIndex:index];

        [slideList addObject:slide];

        index = [indexes indexGreaterThanIndex:index];
    }

    if ([slideList count]) {
        //remove the slides from the master list

        [self recordSlideChange];

        [mSlides removeObjectsInArray:slideList];

        [self notifySlidesChanged];
    }
}

推荐答案

NSUInteger可能大于unsigned int,这可能取决于构建目标(32位与64位,LP64与ILP64).来自 NSUInteger :

NSUInteger could be bigger than unsigned int, and this can depend on the build target (32bit vs 64bit, LP64 vs ILP64). From NSUInteger:

#if __LP64__ || NS_BUILD_32_LIKE_64
typedef long NSInteger;
typedef unsigned long NSUInteger;
#else
typedef int NSInteger;
typedef unsigned int NSUInteger;
#endif

如果是您的目标之一,则为NSNotFound,它是等于NSIntegerMax的枚举值(

If that's the case for one of your targets, NSNotFound, which is an enum value equal to NSIntegerMax (see here) won't fit in an unsigned int. So some integer promotion will come into play, and you'll never hit equality (which the compiler is telling you about) on this line:

while (index != NSNotFound) {

index声明为NSUInteger(NSIndexSet用于索引的类型),并且该问题应轻而易举地解决.

Declare index as an NSUInteger (the type used by by NSIndexSet for the indexes) and that problem should be solved portably.

这篇关于警告:由于数据类型的范围有限,比较总是正确的,这会导致崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆