Matter of data types

Discussion in 'Mac Programming' started by sam256, Oct 4, 2011.

  1. sam256 macrumors newbie

    Joined:
    Oct 4, 2011
    #1
    What is the difference benween int and NSNumber? Is NSNumber an analog of th object type in C#?
    Tell me please in what cases it would be better to use NSInteger type and in what cases NSNumber*?
     
  2. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #2
    int is a primitive data type.
    NSInteger is a typedef for a primitive data type.
    NSNumber is an Objective-C class.

    Lets say you want to write a function that takes two integer numbers as parameters, calculates their product, and prints the result. Write that function, then write a main function that uses your function to print the product of 12 and 59.

    After that, create an NSDictionary that has the strings "width" and "height" as keys with values 2000 and 1500.

    When you've done both, answer the question yourself.
     
  3. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #3
    I'm not a C# guy, but from what I just read the "value types" (I call them primitives) are all aliased to structs in the System class. Primitives will be autoboxed and unboxed to these structs as context warrants. Java behaves this way, too, having wrappers for primitives to/from which it will autobox. In Objective-C NSNumber wraps just about any primitive (no pointers, I believe), but it's manual unlike the other two languages discussed here. Go through what gnasher729 suggested (maybe do so in C#, too) and see what you get.

    -Lee
     
  4. sam256 thread starter macrumors newbie

    Joined:
    Oct 4, 2011
    #4
    gnasher729, lee1210, thank you for you responsivness!! you answers have helped me a lot!!
     
  5. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #5
    I think you could convince NSNumber to wrap a pointer if you cast the pointer to a primitive, but the result would be pretty useless for much of anything.
     
  6. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #6
    NSValue would probably be better, it knows what a pointer is.

    -Lee
     
  7. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #7
    But NSNumber is a subclass of NSValue, so it inherits "knowledge" of pointers.
     
  8. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #8
    You would still not wrap a pointer, you would store a memory address in a primitive.
     

Share This Page