4

hi i am new to obj C and i am assigning a text field value to int variable PaidLeaves as below: because text field return string value i have to cat it to int value so i use following code: for example

PaidLeaves = txtPaidLeaves.text.intValue;

and

PaidLeaves = txtPaidLeaves.text.integerValue;

above i am assigning a text field value to int value

and both works but what is difference between two expression

please tell me thank you

chris
  • 16,324
  • 9
  • 37
  • 40
Devil Decoder
  • 966
  • 1
  • 10
  • 26
  • 1
    One returns `int`, the other returns `NSInteger`. Use the one that actually matches the type you need. – rmaddy Apr 01 '17 at 16:19
  • Related http://stackoverflow.com/questions/6056754/what-is-the-difference-between-int-and-nsinteger Not sure if exact duplicate. – Sulthan Apr 01 '17 at 17:22

1 Answers1

10
  • intValue returns an int number.
  • integerValue returns a NSInteger number.

The difference between them is their number of bits, or in easier terms, the range of values that they can store. Has said in an answer of a different question:

int is always 32-bits.

long long is always 64-bits.

NSInteger and long are always pointer-sized. That means they're 32-bits on 32-bit systems, and 64 bits on 64-bit systems.

Reference: https://stackoverflow.com/a/4445467/4370893

Consider that Apple has only made 64 bit systems since Mac OS X 10.7 (Lion), which was released in 2011, so I'm gonna refer to NSInteger has a 64 bit long integer.

So what that means?

The first bit of a signed integer number, like NSInteger and int, is used to define if it's a positive or a negative number. The conclusion is that a signed integer number goes from -2^(number of bits-1) to 2^(number of bits-1)-1, so...

  • int: - 2,147,483,648 (- 2^31) to 2,147,483,647 (2^31-1)
  • NSInteger: - 9,223,372,036,854,775,808 (- 2^63) to 9,223,372,036,854,775,807 (2^63-1)
Community
  • 1
  • 1
VitorMM
  • 1,060
  • 8
  • 33