I have the following property:
@property (nonatomic, assign) NSDecimalNumber askingPrice;
I am trying to set the value from a textfield:
myManager.askingPrice = [self.askingPriceTextField.text doubleValue];
I get the following warning:
Assigning to 'NSDecimalNumber' from incompatible type 'double'
Also when I try getting the value like:
[NSDecimalNumber numberWithDouble:myManager.askingPrice];
I get error:
Sending 'NSDecimalNumber' to parameter of incompatible type 'double'
I have not worked with NSDecimalNumber before but have Google'd and seen that it is to be used when dealing with currency. Can someone point out a helper function I can use that would allow me to do what I am trying to do?
thanks
Your askingPrice
property in myManager
is a NSDecimalNumber
, a Cocoa class that wraps an object around decimal numbers. Your askingPriceTextField.text
gives you an NSString
that you can convert to a float
or double
.
So basically what you want to do is build a new NSDecimalNumber
using the NSString
in your text field. Here's how you can do it:
double askingPriceDouble = [self.askingPriceTextField.text doubleValue]; // extract a double from your text field
myManager.askingPrice = [[NSDecimalNumber alloc] initWithDouble:askingPriceDouble]; //create an NSDecimalNumber using your previously extracted double
I'm using the alloc-init scheme here because you set your property to be assign
ed. Since an NSDecimalNumber
is an object (not a basic type like double
), you'll either need to make sure your asking price is retain
ed so it isn't deallocated before you need it. Another way of doing this would be to declare your property like this:
@property(nonatomic, retain) NSDecimalNumber *askingPrice; // Do not forget the '*' sign when declaring object properties as pointers.
And then:
double askingPriceDouble = [self.askingPriceTextField.text doubleValue];
myManager.askingPrice = [NSDecimalNumber numberWithDouble:askingPriceDouble];
This'll work because your askingPrice property will be retain
ed when you set it.
Also, don't forget to make sure that your text field's content can actually be converted to a double...