Re: Can an NSArray ever have a count of -1?
Re: Can an NSArray ever have a count of -1?
- Subject: Re: Can an NSArray ever have a count of -1?
- From: Jens Alfke <email@hidden>
- Date: Fri, 19 Feb 2016 14:29:03 -0800
> On Feb 19, 2016, at 1:44 PM, Jean-Daniel Dupas <email@hidden> 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 (email@hidden)
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:
This email sent to email@hidden