Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there an NSInteger?

Tags:

objective-c

If NSInteger is just like a regular int then why does it exist and what is its purpose in being called NSInteger?

I'm new to Mac OS X programming and we'll be having a report for this.

like image 915
Vincent Bacalso Avatar asked May 03 '11 14:05

Vincent Bacalso


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.

Why use NSNumber?

The purpose of NSNumber is simply to box primitive types in objects (pointer types), so you can use them in situations that require pointer-type values to work. One common example: you have to use NSNumber if you want to persist numeric values in Core Data entities.

Is NSInteger primitive?

As said by others before, NSNumber is an NSObject subclass. It is not a C primitive (like int, unsigned int, float, double, etc.) NSInteger , CGFloat , NSUInteger are simple typedefs over the C primitives. The need for NSNumber arises from the need to use numbers as parameters to APIs that require Objects.

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 .


2 Answers

It's an architecture-safe (64 vs 32 bit) type to support different platforms and implementations of C.

Apple recommends that you use NSInteger over normal types anyway, I would assume for portability!

You can read more at this Foundation Types Reference.

Basic description:

When building 32-bit applications, NSInteger is a 32-bit integer. A 64-bit application treats NSInteger as a 64-bit integer.

like image 174
Rudi Visser Avatar answered Oct 13 '22 13:10

Rudi Visser


Other than the typedef being different on different systems (long on 64-bit systems, int on 32-bit), there isn't much of a reason.

Arguably, it gives the impression that an NSInteger is an object, when it's not.

like image 35
Wooble Avatar answered Oct 13 '22 15:10

Wooble