2011-01-05 42 views
0

我的NSTextField包含雙打像「1.67」或「0.05」等如何獲取NSTextField中的值?

當我嘗試以下方法:

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等於0始終,即使價值是另一回事。我怎樣才能正確地獲得priceChange的價值?

+0

console.log()?這是Objective-C還是Objective-J?卡布奇諾咖啡或可可粉? – d11wtq 2011-01-05 06:51:43

+0

只是一個想法,但'priceChange'零,不知何故? – d11wtq 2011-01-05 06:53:03

回答

0

這正是NSNumberFormatter的用途。給你的領域一個合適的格式化器(易於在IB中添加/設置),-objectValue將開始返回NSNumber

0

嘗試[[priceChange stringValue] floatValue]。還要確保字符串中的數字之前沒有其他字符,否則floatValue將返回0.

+0

'[priceChange stringValue] floatValue]'results in NaN – 2011-01-05 06:35:56

+0

文本字段的值是什麼?如果它太大而不適合浮動,則可以嘗試doubleValue。 – ughoavgfhw 2011-01-05 19:03:36