Sam
Sam

Reputation: 621

NSInteger integer initialisation

Is there is no needs of declaring NSInteger using the alloc and initialise keyword? and why?

I was trying doing NSInteger *choice = [[NSInteger alloc] init]; while I found direct assignment

Upvotes: 1

Views: 10596

Answers (2)

Sergey  Pekar
Sergey Pekar

Reputation: 8745

NSInteger is just typedef of int or long (depends on platform)

So you can initialize something like that

NSInteger i = 10;

Quote from SDK documentation

NSInteger Used to describe an integer.

typedef long NSInteger;

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/library/ios/documentation/cocoa/reference/foundation/Miscellaneous/Foundation_DataTypes/Reference/reference.html

Upvotes: 3

Michael Dautermann
Michael Dautermann

Reputation: 89519

NSInteger is not an Objective-C object. It's a datatype (much like C's int or char types). You do not need to call "alloc" (to explicitly allocate memory space) for it.

You can read up on the other Objective-C data types (like NSRange, which you'll use a lot of once you get into NSString objects) in this Apple documentation.

Upvotes: 1

Related Questions