-3

This is from debug.

url String  "https://openweathermap.org/img/w/Optional(\50n\).png"  

The problem is in this line:

 self.imgURL = "https://openweathermap.org/img/w/\(self.dodatek).png"

When I change (self.dodatek) for example to icon 50n it works and show me the icon. When I start my weather app and write name of the city I want to have url like this, but for 50n it has to be my variable that is taken from json.

https://openweathermap.org/img/w/50n.png
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Ciechanx
  • 1
  • 1

1 Answers1

0

Looks like the quick and dirty fix for your problem is unwrapping the optional dodatek(which is probably String?) like this

 self.imgURL = "https://openweathermap.org/img/w/\(self.dodatek!).png"

The cleaner solution is definitely

guard let dodatek = self.dodatek else {
    // handle that dodatek is nil
    // or just return
    return
}
 self.imgURL = "https://openweathermap.org/img/w/\(dodatek).png"

Explanation

The problem is that your property dodatek can theoretically be nil when you declare it as String? If you are sure that it can never be nil, declare it as

var dodatek: String // not String?

instead. In case it can be nil, the guard let statement above should be used to either define a fallback value of an url that should be used (like a url to a generic weather icon maybe)

MarkHim
  • 5,686
  • 5
  • 32
  • 64