views:

38

answers:

3

Hi All,

I'm trying to get the value of a UISlider and store it into part of a CoreData entity.

The score I need to save will either be the value of the slider (i.e. slider.value) or 10 minus the value of the slider (i.e. 10 - slider.value)

I'm trying to put it into an NSNumber and when I use the intValue it works fine.

When I use either...

score = [NSNumber numberWithInt:slider.value];

or...

score = [NSNumber numberWithInt:10 - slider.value];

it works perfectly and I can recall the values at a later date with no problems.

However, I really need to get the value to 1 decimal place but whenever I try this I just can't get it to work.

I really don't even know where to start. I can NSLog the doubleValue out with no problems but I can't figure out how to get this into an NSNumber.

I seem to always get the error "Cannot convert to a pointer type (2)"

Any help is much appreciated.

Thanks

+3  A: 

Have you tried numberWithFloat:? If you're using an int, you will of course never get any decimal places because an integer by definition is a whole number.

Tom Irving
LOL, I'm almost certain that I tried that. Oh well, it's working now! Thanks!Will accept the answer in 5 mins (when timer runs out).
Fogmeister
+2  A: 

If you can NSLog the doubleValue out with no problems then the following method should work:

- (id)initWithDouble:(double)value
ennuikiller
Thanks! This also worked but I have given the answer to Tom as his reply was first.Thanks again!
Fogmeister
+2  A: 

UISlider -value: is of type float, so 10 - slider.value gives a float, and numberWithInt: is expecting an int (which might cast automatically), but as you want a float you should use numberWithFloat: anyway.

But your error "Cannot convert to a pointer type (2)" is not precision-related, it's more a pointer/value thing. Is score of type NSNumber *? Or is it just a plain float/double? What line gives this error? Maybe give some more code.

Eiko
Thanks, I was trying to use NumberWithFloat:[slider.value floatValue]This was causing the error. Sorted now though :DThanks!
Fogmeister