Comparing integers with warning of different signs with Xcode

I use open source to create my project. when I add an EGOTextView to the project, it has semantic problems, for example:

 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 the source code:

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

I noticed that the project has an assembly configuration 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 assume this is causing problems. However, I do not know the value for this setting OTHER_CFLAGS . And I also don’t know how to fix it in order to avoid semantic problems.

Can anyone help me?

Thanks!

+4
source share
4 answers

The configuration option you are looking at will not give anything about the warning you specified. What you need to do is go into your build settings and search for dating alerts. Turn it off.

enter image description here

+4
source

Actually, I don’t think that disabling the compiler warning is the right solution, since comparing int and unsigned long is a subtle error.

For instance:

 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 discard (implicitly or explicitly) an unsigned int into an int , your code will behave incorrectly if the value of your unsigned int greater than INT_MAX.

The correct solution is to discard signed int to unsigned int , and also compare signed int with zero, covering the case when it is negative:

 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 } 
+18
source

Instead of doing all this weird behavior, you should first notice why you are comparing different types in the first place: YOU CREATE INT!

do it instead:

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

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

+5
source

Instead of turning alerts, you can also prevent them from appearing.

Your lines.count file is of type NSUInteger. Do an int of this first, and then do a comparison:

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

Source: https://habr.com/ru/post/1384243/


All Articles