> On Feb 19, 2016, at 1:44 PM, Jean-Daniel Dupas <mail...@xenonium.com> wrote:
> 
> Not exactly. %d is for 32 bit signed integer,

Not exactly ;) %d is for “int”, whose size is unspecified. It does happen to be 
32 bits on Apple platforms with current compilers. (I still remember the “fun” 
period of the early ‘90s when some Mac C compilers had 16-bit ints and some had 
32-bit.)

> but %ld is for signed long, and so is the right formatter for NSInteger value 
> (which is a typedef alias of long) and 64 bit integer on 64 bit platform.

NSInteger is a typedef of ‘long’ in 64-bit, and ‘int’ in 32-bit.
You’re correct that %d should be used for NSInteger in 32-bit.

The whole variable-size NS[U]Integer thing is awful, especially for anyone 
writing code that needs to support both 32-bit and 64-bit. One trick I recently 
heard about that helps somewhat is to use %zd and %zu to format it — the ‘z’ 
modifier indicates the value is a size_t or ssize_t, and it turns out those 
have the same size as NS[U]Integers.

(Here’s one reason NSInteger sucks: the difference in sizes doesn’t make sense 
for values that don’t refer to memory sizes. For example, is it OK use 
NSUInteger to store a file size? In a 64-bit process, sure! In a 32-bit one, 
you’ll be fine until you encounter a file > 4GB long, then you overflow and get 
the size wrong. Likewise, how about using NSUInteger to store an index into a 
persistent array? Bad idea if the persistence layer supports >4 billion items, 
which any real database does.)

—Jens
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to