我有一个带有键和值的json作为 “ average_cost_for_two”:20
当我想在用户界面中显示此内容时,我被显示为两个的平均费用:20 但是,由于Json中的值为Int,因此无法将值转换为String以追加“两个人的平均成本”。
基本上我想这样添加到已发布的var中
for i in fetch.nearby_restaurants{
DispatchQueue.main.async {
self.datas.append(datatype(id: i.restaurant.id, name: i.restaurant.name, image: i.restaurant.thumb, rating: "Rating: " + i.restaurant.user_rating.aggregate_rating, cost_for_two: "I want to add my string to show in View here " + i.restaurant.average_cost_for_two, webUrl: i.restaurant.url))
}
}
附近的餐厅的关键字为“ average_cost_for_two”:整数
任何帮助将不胜感激。谢谢!!
EDIT: looking at Swift questions elsewhere, this may not be the best approach, from what I found there's a built in method
toString
allowing for casting to string. (source: https://stackoverflow.com/a/28203312/2932298) it also seems to vary between versions. Some reference adescription
property after the int:let x = 10.description
, although this is getting language-specific.来自没有Swift经验的人,我不确定是否能够正确回答这个问题。
但是,作为基本原理,大多数(如果不是全部)语言都支持int->字符串转换,因此大多数数据类型都可以非常简单地转换为String。
An idea would be something like:
String(average_cost_for_two)
, converting the int value into a String allowing you to concatenate the values.我相信技术术语将是类型转换,大多数语言对此都支持。同样,特别是对于X->字符串转换。
同样,没有Swift经验,只是来自我所开发的不同语言的基本编程原理。