Re: What is the default type for an integer literal (as relates to its use in NSLog)?
Re: What is the default type for an integer literal (as relates to its use in NSLog)?
- Subject: Re: What is the default type for an integer literal (as relates to its use in NSLog)?
- From: Nick Zitzmann <email@hidden>
- Date: Mon, 15 Dec 2008 13:00:48 -0700
On Dec 15, 2008, at 12:35 PM, Stuart Malin wrote:
I am trying to be 32/64 bit "clean" in some new code that I am
writing. When I declare some integer values, say for named option
values as shown here, what is the type that the compiler assigns to
these values?
enum {
SomeOptionValue = 1,
AnotherOptionValue = 2,
};
Enums are 32-bit constant integers by default. Typedef enums are 32-
bit variable integers. This has not changed in the transition, mainly
because it says in the ANSI C spec that they are the same size as int.
Apple worked around this by making the old typedef enums in the Tiger
SDK into typedef NSIntegers.
Separately, from a space efficiency perspective, would it be better
in the case of having a small set of option values to force the type
to be an unsigned int? That is, if I have a method that takes such
an option, which of the following method signatures is nowadays
preferred?
- (void) someMethodWithOption:(unsigned int)optionValue;
- (void) someMethodWithOption:(NSInteger)optionValue;
Neither. :) You should instead use:
- (void)someMethodWithOption:(NSUInteger)optionValue;
Note the "U" in NSUInteger means unsigned. But you should not use
unsigned int anymore, because it will truncate 64-bit integers, which
may lead to problems. For example, if you're working with the return
value of -[NSArray indexOfObject:], the value of NSNotFound changed
between 32-bit and 64-bit, and your code needs to be ready for this.
(Oh, and if you require an argument that is guaranteed to be no larger
than 32 bits in size, then you should use u_int32_t, not unsigned int.)
I'd suggest turning on the implicit 64-to-32 conversion warning, then
build. That will tell you where there may be trouble.
Nick Zitzmann
<http://www.chronosnet.com/>
_______________________________________________
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