NSNumber Literals

前端 未结 5 1603
南方客
南方客 2020-12-18 09:32

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 suc

相关标签:
5条回答
  • 2020-12-18 09:45

    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.

    0 讨论(0)
  • 2020-12-18 09:45

    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.

    0 讨论(0)
  • 2020-12-18 09:49

    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.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-18 10:00

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

    [Tyler setArms:[NSNumber numberWithInt:1]];
    
    0 讨论(0)
提交回复
热议问题