开发者

How can I get the value inside of an NSTextField?

开发者 https://www.devze.com 2023-02-02 05:55 出处:网络
My NSTextField contains doubles like \"1.67\" or \"0.05\" etc. When I try the following: console.log([priceChange doubleValue]); //priceChange is an NSTextField

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 pr开发者_运维技巧iceChange?


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.


Try [[priceChange stringValue] floatValue]. Also make sure there is nothing other than whitespace before the number in the string, or floatValue will return 0.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号