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?
4 Solutions Collect From Internet About “int vs NSNumber vs 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.
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.
NSInteger is a simply an integer,
NSNumber is an object where as
int is a primitive data type.
NSInteger is nothing more than a synonym for a long integer.
If you need to store a number somewhere, use NSNumber. If you’re doing calculations, loops, etc, use NSInteger, NSUInteger or int.
You can wrap an NSInteger into an NSNumber using:
NSNumber *NumberToStore = @(21);
and get it back using:
NSInteger retrievingInteger = [NumberToStore integerValue];
1 is a literal and the compiler will make it the appropriate numeric type if that’s appropriate.
Note, however, that while
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];
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
- Facebook SDK v4.0 for iOS – FBSDKProfile currentProfile not being set
- Swift, how to play sound when press a button
- How can I separate testing and production data in my Firebase Database?
- Parent UIViewController Orientation Shouldn't Change After Dismissing the Child
- How can you enable the iOS 6.0 panorama camera within an application?
- Does IOS support all Unicode emojies?
- How to query “Or ” with includeKey in parse?
- How to fix xcode bug “web filter is active”?
- Xcode: entering user input when debugging
- Memory leak: steady increase in memory usage with simple device motion logging
- UIDatePicker bug? UIControlEventValueChanged after hitting minimum internal
- Compose UIBarButtonItem changes position slightly when coming into view
- RealmSwift: Convert Results to Swift Array
- xcode 8.3 error: The specified item could not be found in the keychain
- What will happen if iOS distribution certificate expired?