0

now, when I try to nslog my array count using self.array.count with %d I receive this messageL:Values of type "NSUInteget" Should be not be used as format argumments and it suggest s that I fix it with %lu instead,. is this documented anywhere?

Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
vboombatz
  • 409
  • 6
  • 17
  • 1
    possible duplicate of [Why does an NSInteger variable have to typecasted to type long?](http://stackoverflow.com/questions/16075559/why-does-an-nsinteger-variable-have-to-typecasted-to-type-long) – Martin R May 07 '14 at 15:30

2 Answers2

1

NSArray's count method returns an NSUInteger, as documented. If you're using a 64 bit environment, it's also documented that those require the format of %lu.

If you were using a signed NSInteger instead, you would need to use %ld in a 64 bit environment, or %d in a 32 bit environment.

FreeAsInBeer
  • 12,937
  • 5
  • 50
  • 82
0

You are probably seeing the message now because you have updated your compiler, Xcode is doing more checking and issuing more warnings.

The issue here is that the size of NSUInteger (and NSInteger) is different on different platforms. Rather than add a new format specifier to handle this Apple choose to recommend that the format specifier for the largest possible type is used and a cast. So:

  1. For NSUInteger use the format specifier %lu and cast the value to unsigned long; and
  2. For NSInteger use the format specifier %ld and cast the value to long.

Doing this will produce correct results on both 32-bit and 64-bit platforms.

See Platform Dependencies in String Format Specifiers.

CRD
  • 52,522
  • 5
  • 70
  • 86
  • On the other hand, NSArray's `count` (in my lifetime at least) will never exceed 2*31, so simply casting to `(int)` and using `%d` should be sufficient for all but the nit-pickers. – Hot Licks May 07 '14 at 17:43
  • @HotLicks - Yes, you could argue either way. As platforms are tending to 64-bit casting to `unsigned long` will on most occasions be a no-op and no-risk, so Apple's recommendation makes sense. – CRD May 07 '14 at 19:25