Question

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?

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top