35

I need to store the maximum value of an NSInteger into an NSInteger? What is the correct syntax to do it?

Thanks.

bmargulies
  • 97,814
  • 39
  • 186
  • 310
David
  • 14,205
  • 20
  • 97
  • 144

4 Answers4

92

The maximum value of an NSInteger is NSIntegerMax.

Patroclus
  • 1,163
  • 13
  • 31
Chuck
  • 234,037
  • 30
  • 302
  • 389
  • 7
    @Mark: on *current* implementations. Future implementations could conceivably choose a different value, though one hopes not. – Jonathan Grynspan Jan 26 '11 at 01:15
  • 1
    @Cfr: Thanks for the link, but that number is not right. On 32-bit systems, NSInteger cannot represent 4294967295 (the number you gave is the maximum value of an **unsigned** 32-bit integer), and on 64-bit systems it can go a lot higher than that. – Chuck Jun 04 '13 at 17:54
  • 11
    Oh sorry, actually I was searching for `NSUIntegerMax`/`ULONG_MAX` and found this answer. `NSUIntegerMax` is `4294967295` while `NSIntegerMax` is `2147483647` on 32-bit systems. Sometimes the value itself is useful, for example, when you want to understand whether it is possible to overflow the ID typed as integer. – Cfr Jun 04 '13 at 21:02
  • 2
    here is nice [list of max/min values](http://stackoverflow.com/questions/2107544/types-in-objective-c-on-iphone/2107549#2107549) – Cfr Jun 04 '13 at 21:04
  • If someone was looking for the min, it's, as you'd expect, `NSIntegerMin` – rounak Dec 23 '14 at 11:21
13

The maximum value for an NSInteger is NSIntegerMax

(from Foundation Constants Reference)

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
4

For 32-bit & 64 bit, there are two conventions: a)ILP32 b)LP64

The 32-bit runtime uses a convention called ILP32, in which integers, long integers, and pointers are 32-bit quantities. The 64-bit runtime uses the LP64 convention; integers are 32-bit quantities, and long integers and pointers are 64-bit quantities. These conventions match the ABI for apps running on OS X (and similarly, the Cocoa Touch conventions match the data types used in Cocoa), making it easy to write interoperable code between the two operating systems.

Table 1-1 all of the integer types commonly used in Objective-C code. Each entry includes the size of the data type and its expected alignment in memory. The highlighted table entries indicate places where the LP64 convention differs from the ILP32 convention. These size differences indicate places where your code’s behavior changes when compiled for the 64-bit runtime. The compiler defines the LP64 macro when compiling for the 64-bit runtime.

enter image description here

for 64 bit max range for NSInteger is : LONG_MAX : 9223372036854775807

Ash
  • 5,525
  • 1
  • 40
  • 34
2

Took me a little while for me to realise why I was getting a different value from NSIntegerMax when using NSUInteger!!

And the maximum for a NSUInteger is NSUIntegerMax

(also from http://developer.apple.com/library/ios/#documentation/cocoa/reference/foundation/Miscellaneous/Foundation_Constants/Reference/reference.html)

Andy B
  • 629
  • 8
  • 15