我在将变量添加到url时遇到了问题。在代码中我有这样的代码:
self.imgURL = "https://openweathermap.org/img/w/\(self.dodatek).png"
这不管用。在调试器中,它向我显示以下内容:
url String "https://openweathermap.org/img/w/Optional(\50n\).png"
但是它应该是这样的:
https://openweathermap.org/img/w/50n.png
当我将代码更改为以下代码时:
self.imgURL = "https://openweathermap.org/img/w/50n.png"
它可以工作,并向我显示天气图标,但我想将我的变量放在那里,该变量从json中获取图标名称。
发布于 2017-01-29 21:41:19
看起来self.dodatek是一个Opional
。你需要把它打开。我建议使用if let
可选绑定,或者使用guard语句:
if let filename = self.dodatek {
self.imgURL = "https://openweathermap.org/img/w/\(filename).png"
}
else {
print("Error. filename in self.dodatek is nil!")
return
}
发布于 2017-01-29 21:39:46
您的self.dodatek
似乎是一个可选的值。您需要通过编写self.dodatek!
对其进行解包,因此您的字符串应该是:
self.imgURL = "https://openweathermap.org/img/w/\(self.dodatek!).png"
https://stackoverflow.com/questions/41926233
复制相似问题