How can I get the value inside of an NSTextField?
-
25-09-2019 - |
Question
My NSTextField contains doubles like "1.67" or "0.05" etc.
When I try the following:
console.log([priceChange doubleValue]); //priceChange is an NSTextField
//Apply styles
if([priceChange doubleValue] > 0)
[priceChange setTextColor:[NSColor greenColor]];
else if([priceChange doubleValue] < 0)
[priceChange setTextColor:[NSColor redColor]];
else if([priceChange doubleValue] == 0)
[priceChange setTextColor:[NSColor blackColor]];
priceChange is equal to 0 always, even when the value is something else. How can I properly get the value of priceChange?
Solution
This is precisely what NSNumberFormatter
is for. Give your field a suitable formatter (easy to add/setup in IB), and -objectValue will start returning an NSNumber
.
OTHER TIPS
Try [[priceChange stringValue] floatValue]
. Also make sure there is nothing other than whitespace before the number in the string, or floatValue will return 0.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow