The advantages of NSNumber etc?

Discussion in 'Mac Programming' started by MorphingDragon, Apr 11, 2010.

  1. MorphingDragon macrumors 603

    MorphingDragon

    Joined:
    Mar 27, 2009
    Location:
    The World Inbetween
    #1
    What are the advantages of using NSNumber etc? (Apart from treating them like objects) Like memory management or performance benefits? Because its a lot of code when I simply would require a local variable; int, float, char etc.
     
  2. GorillaPaws macrumors 6502a

    GorillaPaws

    Joined:
    Oct 26, 2003
    Location:
    Richmond, VA
    #2
    The main purpose is that they're objects so they can be used in places where primitive types can't be used such as in NSArrays, etc.
     
  3. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #3
    Store them as primitives in your app when you can since NSNumber is immutable, and only use NSNumber when necessary (storing in an NSDictionary, NSUserDefaults, or any other API that requires an object, etc).
     
  4. GorillaPaws macrumors 6502a

    GorillaPaws

    Joined:
    Oct 26, 2003
    Location:
    Richmond, VA
    #4
    Technically we're supposed to be using NSInteger, NSFloat etc. though (Which are primitives, just defined for the appropriate architecture), right?
     
  5. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #5
    Obviously storing them in NSArrays and NSDictionaries, but also a much easier way to write them into a file since NSArray and NSDictionary can be serialized into a plist file as can NSNumber and NSString etcetera.
     
  6. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #6
  7. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #7

Share This Page