Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Objective-C when can I use a primitive data type instead of NSNumber?

Tags:

objective-c

I was recently writing a program in Objective-C and I tried the wrote the following part of a line where 'someString' is an instance of NSString:

[someString substringFromIndex:3];

The compiler seems okay with this but I'm not exactly sure why. The documentation for the substringFromIndex method says that the parameter has to be an NSUInteger however, isn't 3 just a primitive integer? Why can I do this? (Note, I'm new to Objective-C so I'm sure the reasoning is very simple or I'm just wrong about something.)

like image 436
Nosrettap Avatar asked Nov 23 '25 23:11

Nosrettap


1 Answers

NSUInteger is typedef'd to a primitive type. Which type it is depends on the processor you're compiling for.

As seen here (and pasted below):

#if __LP64__ || TARGET_OS_EMBEDDED || TARGET_OS_IPHONE || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64 
    typedef unsigned long NSUInteger;
#else 
    typedef unsigned int NSUInteger;
#endif
like image 95
Cajunluke Avatar answered Nov 28 '25 17:11

Cajunluke



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!