I have a line of code that will work differently depending on the datatypes "day" and "1". I believe it is the following although I will check my source code later.
day = day + 1;
Does this make sense? What would the differences be?
I have a line of code that will work differently depending on the datatypes "day" and "1". I believe it is the following although I will check my source code later.
day = day + 1;
Does this make sense? What would the differences be?
1
is a literal and the compiler will make it the appropriate numeric type if that's appropriate.
Note, however, that while NSInteger
and int
are to most intents and purposes the same thing, NSNumber
is an object type. For that case, your code doesn't make sense (and shouldn't compile without warnings).
(Actually, there are some circumstances where it would make a kind of sense, in terms of pointer arithmetic, but that's totally not what you want.)
For the NSNumber case, you'd instead want something like:
day = [NSNumber numberWithInt:[day intValue] + 1];
NSInteger
and int
are equivalent, NSNumber
is an object used to store primitive number types (int, long, float, etc.).
The example code you posted will work just fine if day
is an NSinteger
, it won't work at all if it's an NSNumber
.
NSInteger
is a type definition that describes an integer - but it is NOT equivalent to int
on 64-bit platforms.
You can examine the typedef by cmd-clicking on NSInteger
in Xcode.
NSInteger
is defined as int
when building a 32-bit app and as long
for 64-bit apps.
Most of the time you can replace int with NSInteger, but there are some things to consider when doing so.
Apple's 64-Bit Transition Guide for Cocoa has some information about that.
NSNumber is a class that helps you to store numeric types as object. It has methods to convert between different types and methods to retrieve a string representation of your numeric value.
If you use a variable day
of type NSNumber*
the way you did in your example, you are not modifying the value of day
but its memory address.
If you are working with time values you also could take a look at NSDate.