NSNumber won't become float

Discussion in 'Mac Programming' started by liptonlover, Sep 5, 2008.

  1. macrumors 6502a

    Joined:
    Mar 13, 2008
    #1
    By using those lovely NSLogs, I have found that my NSNumber instance won't become a float. The variable is definitely a float, and it shows up as 100.00. But the NSNumber shows up as 100. Here's the assignment code.
    returnValue=[[NSNumber alloc] initWithFloat:animateFloat];
    Should this not make returnValue (my NSNumber instance) a float value, the value of animateFloat? (Which is of course a float variable)
     
  2. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #2
    An NSNumber contains a number; it doesn't say whether it is an integer, a float, or a double value. Whether you create an NSNumber with a float, double or integer value 100 doesn't make any difference. They are exactly the same thing.

    Just wondering: Do you have any good reason to use float instead of double? I know tons of good reasons for preferring double.
     
  3. Guest

    caveman_uk

    Joined:
    Feb 17, 2003
    Location:
    Hitchin, Herts, UK
    #3
    Have you tried seeing what you get when you do [returnValue floatValue]? NSNumber doesn't 'become' a float - it's always an NSNumber regardless what you stuff into it. Try sticking a value like 1.5 into it and you'll see it stores it.

    Incidently returnValue sounds like a return value from a method. It is usually assumed that return values from methods are autoreleased objects. You should be using
    Code:
    NSNumber* returnValue=[NSNumber numberWithFloat:animateFloat];
    
    if that's the case.
     
  4. thread starter macrumors 6502a

    Joined:
    Mar 13, 2008
    #4
    here's what I did.

    1. Checked the value of my float, which was 100.00
    2. Assigned the value of my float to returnValue
    3. In another method, checked the value of returnValue, which was 100
    4. Printed the value of returnValue, which printed 100

    I'm using a double too... I have a different method for each basic C data type. I just haven't gotten to doubles yet.

    So making the number a .5 value printed it properly... so NSNumber is just being "smart" I guess... thanks!

    Oh yeah... I use garbage collection :)
     
  5. macrumors regular

    Joined:
    Nov 30, 2005
    #5
    [returnValue floatValue];
     

Share This Page