Comparison of integers of different signs warning with Xcode

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

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


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

Could any one help me?



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.

Actually, I don't think turning off the compiler warning is the right solution, since comparing an int and an unsigned long introduces a subtle bug.

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 int to an int your code will behave incorrectly if the value of your unsigned int is greater than INT_MAX.

The correct solution is to cast the signed int to unsigned int and to also compare the signed int to zero, covering the case where 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

Need Your Help

I've been using Authlogic, but now need to remove all traces of it. What do I need to do?

ruby-on-rails ruby-on-rails-3 devise user authlogic

So I'm going to switch from Authlogic to Devise. Since I only have a couple of test accounts, I thought it would be best to simply remove all the Authlogic stuff and my users table, then setup Devi...

Models -> has_many -> Twice

ruby-on-rails model associations has-many has-many-through

So I have a somewhat confusing relationship here, between a Note, Group, and User. And I ended up with has_many twice in my model. But I'm currently focused on the Note &amp; Group relationship.