iPhone UISlider to NSNumber
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 开发者_Python百科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
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.
If you can NSLog the doubleValue out with no problems then the following method should work:
- (id)initWithDouble:(double)value
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.
精彩评论