2012-12-11 78 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