NSNumber Literals
Asked Answered
G

5

4

I am very new to Objective-C. I know C and C++ but Objective-C has quite the learning curve. Anyway, is there a shorter way (possibly by some kind of NSNumber literal if such exists) to write the following:

[Tyler setArms:[[[NSNumber alloc] autorelease] initWithInt:1]];
Gravitt answered 21/3, 2011 at 4:17 Comment(1)
To those who are wondering, Tyler is an instance of class Human that I created.Gravitt
L
6

Yes, just use one of the many helper functions such as numberWithInt::

[Tyler setArms:[NSNumber numberWithInt:1]];

The expression [NSNumber numberWithInt:1] is equivalent to [[[NSNumber alloc] initWithInt:1] autorelease], which is equivalent to [[[NSNumber alloc] autorelease] initWithInt:1]. The latter expression is extremely uncommon.

Lidia answered 21/3, 2011 at 4:22 Comment(2)
I didn't know whether to initialize the object before or after telling it to auto-release. Thanks. :)Gravitt
The latter expression is not only uncommon, it is entirely incorrect! It'll break completely for classes whose -init* method return something other than what was allocated in the first place (like, say, every NSString ever instantiated).Vasti
W
17

As of Clang v3.1 you can now use Objective-C literals.

NSNumber *fortyTwo = @42;             // equivalent to [NSNumber numberWithInt:42]
NSNumber *fortyTwoUnsigned = @42U;    // equivalent to [NSNumber numberWithUnsignedInt:42U]
NSNumber *fortyTwoLong = @42L;        // equivalent to [NSNumber numberWithLong:42L]
NSNumber *fortyTwoLongLong = @42LL;   // equivalent to [NSNumber numberWithLongLong:42LL]

So, answering your specific question:

[Tyler setArms:[[[NSNumber alloc] autorelease] initWithInt:1]];

Can now be written as:

[Tyler setArms:@1];

There are also literals for arrays and dictionaries, but they are beyond the scope of this question.

To take advantage of literals in Xcode you'll need at least version 4.4 (at the time of writing this is a preview only).

NB: LLVM is an open source project so none of this is subject to Apple's NDA.

Wentworth answered 20/6, 2012 at 13:12 Comment(0)
L
6

Yes, just use one of the many helper functions such as numberWithInt::

[Tyler setArms:[NSNumber numberWithInt:1]];

The expression [NSNumber numberWithInt:1] is equivalent to [[[NSNumber alloc] initWithInt:1] autorelease], which is equivalent to [[[NSNumber alloc] autorelease] initWithInt:1]. The latter expression is extremely uncommon.

Lidia answered 21/3, 2011 at 4:22 Comment(2)
I didn't know whether to initialize the object before or after telling it to auto-release. Thanks. :)Gravitt
The latter expression is not only uncommon, it is entirely incorrect! It'll break completely for classes whose -init* method return something other than what was allocated in the first place (like, say, every NSString ever instantiated).Vasti
T
3

You don't have to allocate and initialise, NSNumber provides a convenience method to do that:

[Tyler setArms:[NSNumber numberWithInt:1]];
Treviso answered 21/3, 2011 at 4:23 Comment(0)
X
3

In Xcode 4.4 there are now NSNumber literals:

  // integral literals.
  NSNumber *fortyTwo = @42;             // equivalent to [NSNumber numberWithInt:42]
  NSNumber *fortyTwoUnsigned = @42U;    // equivalent to [NSNumber numberWithUnsignedInt:42U]
  NSNumber *fortyTwoLong = @42L;        // equivalent to [NSNumber numberWithLong:42L]
  NSNumber *fortyTwoLongLong = @42LL;   // equivalent to [NSNumber numberWithLongLong:42LL]

  // floating point literals.
  NSNumber *piFloat = @3.141592654F;    // equivalent to [NSNumber numberWithFloat:3.141592654F]
  NSNumber *piDouble = @3.1415926535;   // equivalent to [NSNumber numberWithDouble:3.1415926535]

  // BOOL literals.
  NSNumber *yesNumber = @YES;           // equivalent to [NSNumber numberWithBool:YES]
  NSNumber *noNumber = @NO;             // equivalent to [NSNumber numberWithBool:NO]

The best docs I've seen so far are in the llvm man page.

X answered 27/7, 2012 at 17:4 Comment(3)
I’ve seen the man page, using Xcode 4.4 now for my project, but the IDE complains that @YES is no good – I checked for the <objc/objc.h> header and it still says #define YES (BOOL)1, instead of __objc_yes. Any ideas what could have gone wrong with the update?Regional
How are you using it? Which version of the SDK is your project built against?X
Base SDK iOS 5.1, Xcode 4.4 (4F250), deployment target iOS 5.0. Could it be the iOS?Regional
S
2

Two things in addition to the previous responses, both of which are correct:

First, it'll be easier for us to help if you follow Cocoa naming conventions: variables, including object pointers, should start with a lower case letter. So, 'tyler' instead of 'Tyler'. Classes and types start with upper case letters.

Second, you'd never autorelease an object before you initialize it. Always alloc first, then init, and then do whatever else you need to do, including release or autorelease.

Snarl answered 21/3, 2011 at 4:25 Comment(1)
Ah, I didn't even realize that I used the capital T. It never occurred to me. Thanks.Gravitt

© 2022 - 2024 — McMap. All rights reserved.