NSUInteger to int

2.1k views Asked by At

I wrote a method tha uses myarray, defined in the same class. When I use count it always returns 0. When I use:

printf("%d", [myarray count]);

compiler says:

Format '%d' expetcs type 'int', but argument 2 has type 'NSUInteger'

why?

1

There are 1 answers

3
Sergey Kalinichenko On BEST ANSWER

You should use %lu instead of %d. The compiler checks your format string against the parameters that you are passing to printf, sees that you are passing an unsigned but print it as a signed integer, and issues a warning. The warning indicates that for numbers greater than or equal to 2^31 printf would output a large negative number, when the data type implies a different semantic, namely, a large positive integer.

EDITED in response to comments by Josh Caswell and thepepp