Semantic Issue: Implicit conversion loses integer precision


#1

In chapter two I got the following warning: Semantic Issue: Implicit conversion loses integer precision

With the following lines of code:

int adjectiveIndex = rand() % [randomAdjectiveList count];
int nounIndex = rand() % [randomNounList count];

To resolve this, I cast them to int as such:

int adjectiveIndex = rand() % (int)[randomAdjectiveList count];
int nounIndex = rand() % (int)[randomNounList count];

I just wanted to run this by the experts to make sure that this is a valid solution.


#2

Yup, that works! (Although the code will run just fine without having to do this, but I don’t like having those pesky warnings either… they weren’t present when this chapter was written.)


#3

I ran into this problem - wasn’t sure how to do casting, thanks for the tip!


#4

Dunno why, but I didn’t get that warning. (Xcode 4.0.2 on Snow Leopard 10.6.8.)


#5

I’m running Xcode 4.1 on Lion 10.7.1 and I did receive the warnings. They don’t flag on compile or stop any of the processes; however, I did receive the yellow caution triangles and the “implicit conversion…” verbiage. Since I had already read this forum post, I ignored them.


#6

int adjectiveIndex=rand()%[randomAdjectiveList count];
int nounIndex=rand()%[randomNounList count];

if the program is ran using above statements (using int), then it gives warning message.

i replace those above 2 statements to following (i.e change from “int” to “unsigned long int”) & the warnings disappeared.

unsigned long int adjectiveIndex=rand()%[randomAdjectiveList count];
unsigned long int nounIndex=rand()%[randomNounList count];

i am using XCode 4.0.2 & OS X 10.6.8