xcode 不同符号警告的整数与Xcode的比较

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8350971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:38:43  来源:igfitidea点击:

Comparison of integers of different signs warning with Xcode

xcodeconfigurationbuild

提问by scorpiozj

I use an open source to build my project. when I add EGOTextViewto the project, it has Semantic Issues like:

我使用开源来构建我的项目。当我添加EGOTextView到项目时,它有语义问题,如:


Comparison of integers of different signs: 'int' and 'NSUInteger' (aka 'unsigned long')
Comparison of integers of different signs: 'NSInteger' (aka 'long') and 'NSUInteger' (aka 'unsigned long')

For example in source code:

例如在源代码中:


     for (int i = 0; i < lines.count; i++)//lines is an array

I notice the project has build configure file which includes:

我注意到该项目具有构建配置文件,其中包括:

// Make CG and NS geometry types be the same. Mostly doesn't matter on iPhone, but this also makes NSInteger types be defined based on 'long' consistently, which avoids conflicting warnings from clang + llvm 2.7 about printf format checking

OTHER_CFLAGS = $(value) -DNS_BUILD_32_LIKE_64

According to the comments, I guess it causes the problems. However, I don't know the meaning for this OTHER_CFLAGSsetting. And I also don't know how to fix it so that it can avoid the semantic issues.

根据评论,我想它会导致问题。但是,我不知道此OTHER_CFLAGS设置的含义。而且我也不知道如何修复它才能避免语义问题。

Could any one help me?

任何人都可以帮助我吗?

Thanks!

谢谢!

采纳答案by SSteve

The configuration option you're looking at won't do anything about the warning you quoted. What you need to do is go into your build settings and search for the "sign comparison" warning. Turn that off.

您正在查看的配置选项不会对您引用的警告执行任何操作。您需要做的是进入构建设置并搜索“符号比较”警告。把它关掉。

enter image description here

在此处输入图片说明

回答by j b

Actually, I don't think turning off the compiler warning is the right solution, since comparing an intand an unsigned longintroduces a subtle bug.

实际上,我认为关闭编译器警告不是正确的解决方案,因为比较 anint和 an 会unsigned long引入一个微妙的错误。

For example:

例如:

unsigned int a = UINT_MAX; // 0xFFFFFFFFU == 4,294,967,295 
signed int b = a; // 0xFFFFFFFF == -1

for (int i = 0; i < b; ++i)
{
    // the loop will have zero iterations because i < b is always false!
}

Basically if you simply cast away (implicitly or explicitly) an unsigned intto an intyour code will behave incorrectly if the value of your unsigned intis greater than INT_MAX.

基本上,如果您简单地(隐式或显式)抛弃 anunsigned int到 an ,int如果您的值unsigned int大于 INT_MAX ,您的代码将表现不正确。

The correct solution is to cast the signed intto unsigned intand to also compare the signed intto zero, covering the case where it is negative:

正确的解决方案是将signed intto 转换unsigned int为并将 thesigned int与零进行比较,涵盖它为负的情况:

unsigned int a = UINT_MAX; // 0xFFFFFFFFU == 4,294,967,295 

for (int i = 0; i < 0 || (unsigned)i < a; ++i)
{
    // The loop will have UINT_MAX iterations
}

回答by i.am.charlie

Instead of doing all of this strange type casting all over the place, you ought to first notice why you are comparing different types in the first place: YOU ARE CREATING AN INT!!

与其到处做这些奇怪的类型转换,您应该首先注意到为什么要首先比较不同的类型:您正在创建一个 INT !!

do this instead:

这样做:

    for (unsigned long i = 0; i < lines.count; i++)//lines is an array

...and now you are comparing the same types!

...现在您正在比较相同的类型!

回答by Brabbeldas

Instead of turning the warnings of you can also prevent them from occurring.

而不是转动你的警告也可以防止它们发生。

Your lines.count is of type NSUInteger. Make an int of this first, and then do the comparison:

您的lines.count 是NSUInteger 类型。先对这个做一个int,然后进行比较:

int count = lines.count;
for (int i = 0; i < count; i++)