4

Am I correct in thinking that declaring a NSInteger in an ios environment means it will only ever be a 32bit value?

I would like to know as I have been told by another programmer not familiar with objective c to use a int32, the only similar thing I can find in objective C is the int32_t, _t standing for 'integer type'.. but working with these types is becoming a real pain I feel like I dont have all the control functionally thats offered from objective c like NSInteger seems to get.

Any help would be greatly appreciated.

HurkNburkS
  • 5,492
  • 19
  • 100
  • 183
  • What kind of control are you getting with NSInteger in Objective-C? – xyzzycoder Aug 09 '12 at 03:18
  • I guess not so much control but ease of use for instance converting strings or other data types seem a fair bit easier that other primitive int types like int32_t.. i.e. **NSInteger myInt = [myString intValue];** hope that makes sense... I have also found out all ARM processors are 32bit.. I did an external search as the documents didnt really specify. – HurkNburkS Aug 09 '12 at 03:21
  • Does the word size matter in your application? I think that you're best off to use the definitions and types that are being returned by Cocoa. I'm uncertain why your developer coworker is advocating something else? – xyzzycoder Aug 09 '12 at 03:22
  • I guess size dose not matter so much, He was just wanting me to make sure I put the data thats being returned into the correct value types.. so if its a string into a NSString a Bool a bool but when it came to the Integer values he suggested a 32 bit int.. and the only thing I could find was int32_t... but I think this is a c value.. so then I was thinking can I use a NSInteger and it be okay, which is why I am asking this question... I guess Im just abit nervious, I want this to be right so im checking things before I commit to much to anything – HurkNburkS Aug 09 '12 at 03:27
  • Lol, well...your "ease of use" approach is incorrect. For NSInteger you should use `integerValue`, not `intValue` (which returns...an int). – borrrden Aug 09 '12 at 03:58
  • I was referring to the fact that say your getting your values from NSString (in this case I am) they have easily accessible accessor methods to convert the string into various number types: doubleValue, floatValue, intValue, integerValue, longLongValue, boolValue. yes my intvalue is wrong 'thankyou for pointing that out' but regarding int32_t types I dont think there are the same quality of accessor methods able to convert their values to this type. – HurkNburkS Aug 09 '12 at 04:08

3 Answers3

8

Here is how "NSInteger" is defined in "NSObjCRuntime.h" in older SDK's:

#if __LP64__ || (TARGET_OS_EMBEDDED && !TARGET_OS_IPHONE) || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64
typedef long NSInteger;
typedef unsigned long NSUInteger;
#else
typedef int NSInteger;
typedef unsigned int NSUInteger;
#endif

As of September 2013, iPhones now can run with 64-bit chips, which means a NSInteger might be much bigger.

In my own coding, if I'm doing pure Objective C, I'll stick with NSInteger since that future-proofs my code for down the line. Open source stuff and certain programmers, on the other hand, love to use "uint32_t" or "int32_t" and other explicit types like this, so when I see those, I try to match their style in the code I'm doing that works with it.

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • thank you, your last sentence is precisely the predicament I face.I have only been developing with objective C for 6 or so months now full time and the project I am working on is a port of an application made by a really accomplished developer (my boss) he works with delphi and other languages. so there is often confusion about the variable types I use as he has never seen them before and I get very nervous when I am asked to explain why I have chosen so now I try very hard to have a complete understanding of my decision. I guess because of my inexperience i have to ask such minor questions. – HurkNburkS Aug 09 '12 at 03:34
  • I don't think it's a minor question. If the other guy insists on "`int32_t`", do what it takes to make him happy (especially if it's your boss). – Michael Dautermann Aug 09 '12 at 03:35
  • Cheers, yea I might I will chat to him in the morning about my findings here (the findings you guys helped me with) and see what he says.. But if its a native alternative that is almost identical to his original suggestion I would like to think he would consider that to be okay... thanks again. – HurkNburkS Aug 09 '12 at 03:59
  • Expect the unexpected. Apple just announced the A7 chip with 64-bit architecture for the new iPhone 5s ;-) – Klaas Sep 10 '13 at 19:51
0

The typedefs exist to shield you from the base type, thats why you use them instead of the raw types its called portability.

deleted_user
  • 3,817
  • 1
  • 18
  • 27
0

Update iOS 11: NSInteger can be 32-bit or 64-bit depending on the application built and the iOS version.

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

https://developer.apple.com/documentation/objectivec/nsinteger

Yuliwee
  • 327
  • 1
  • 11