0

我必须绑定 Json 数据,字符串格式的数据在标签中绑定正常,但我无法在标签中绑定整数或浮点值。是否有任何语法可以在标签中绑定这些类型的值?

        NSDictionary* info = [NSDictionary dictionaryWithObjectsAndKeys:

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

我得到了Name,但我无法绑定Cost(答案是 12.34)。请给我一个继续的想法。

4

3 回答 3

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];
于 2012-12-11T13:04:04.833 回答
-1

您可以将字符串文字的属性用作

showcost.text =[NSString stringWithFormat:@"%f",[[loans objectForKey:@"Cost"]floatValue]];
于 2012-12-11T13:01:02.810 回答
-1

如果 cost 是 float 类型,您可以使用 stringWithFormat 为其创建一个字符串值,以将 Decimal 浮点数打印为字符串:

showcost.text = [NSString stringWithFormat:@"%f", [loans objectForKey:@"Cost"]];
于 2012-12-11T12:57:31.497 回答