2012-12-11 76 views
0

我必須綁定Json數據,具有字符串格式的數據綁定在標籤ok中,但我無法在標籤中綁定整數或浮點值。有沒有在標籤中綁定這些類型的值的語法?將整數值綁定到標籤中

 NSDictionary* info = [NSDictionary dictionaryWithObjectsAndKeys: 

         showcost.text = [loans objectForKey:@"Cost"], 
         showname.text = [loans objectForKey:@"Name"],nil]; 

我收到Name,但我不能綁定Cost(這個問題的答案是12.34)。請給我一個想法繼續。

+0

如何通過代碼或通過IB綁定到UILabel?你是否將它存儲在字典中? –

+0

Iam通過代碼綁定並將其存儲在Dictionary中。 –

回答

-1

如果成本是float類型的,你可以創建一個使用stringWithFormat它一個字符串值打印出十進制浮點數字的字符串:

showcost.text = [NSString stringWithFormat:@"%f", [loans objectForKey:@"Cost"]]; 
+0

現在將值綁定爲'-1.993290',以及警告「格式指定了類型double,但參數具有類型ID」。 –

-1

您可以使用字符串字面作爲

的財產
showcost.text =[NSString stringWithFormat:@"%f",[[loans objectForKey:@"Cost"]floatValue]]; 
+0

對不起,Iam的錯誤是「沒有已知的選擇器實例方法'floatvalue'」。 –

+0

這個答案甚至不會編譯,因爲你試圖給一個'NSString *'類型的屬性賦值'float'。 – rmaddy

0

整數值

int cost = [[loans objectForKey:@"Cost"]intValue]; 
showcost.text = [NSString stringWithFormat:@"%d",cost]; 

浮法值

float cost = [[loans objectForKey:@"Cost"]floatValue]; 
showcost.text = [NSString stringWithFormat:@"%f",cost]; 
+0

您不應該使用字符串格式向用戶顯示數字。相反,使用NSNumberFormatter,以便爲用戶的語言環境正確格式化值。 – rmaddy