Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

int vs NSNumber vs NSInteger

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?

like image 441
Moshe Avatar asked Jun 13 '10 11:06

Moshe


People also ask

What is NSInteger?

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.

Is NSInteger primitive?

NSInteger is just like a traditional int in C. It's a typedef. There are others like NSUInteger , CGFloat , etc. that all are synonyms for primitive types.

What is NSNumber in Swift?

NSNumber is a subclass of NSValue that offers a value as any C scalar (numeric) type. It defines a set of methods specifically for setting and accessing the value as a signed or unsigned char , short int , int , long int , long long int , float , or double or as a BOOL .


1 Answers

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.

like image 73
Thomas Zoechling Avatar answered Oct 24 '22 17:10

Thomas Zoechling