Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

liptonlover

macrumors 6502a
Original poster
Mar 13, 2008
989
0
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)
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
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)

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.
 

caveman_uk

Guest
Feb 17, 2003
2,390
1
Hitchin, Herts, UK
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.
 

liptonlover

macrumors 6502a
Original poster
Mar 13, 2008
989
0
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 :)
 

varsis

macrumors regular
Nov 30, 2005
209
1
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 :)

[returnValue floatValue];
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.