Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Miguel Martin
21,703 PointsError trying to pass a new value into a variable...
After almost finishing the weather app, i want to convert the temperature that comes in the API to celsius..
Then i did a function in the struct like this:
func convertToCelsius() -> Int {
let tempToCelsius = ((temperature - 32) * (5/9))
return tempToCelsius
}
and then i created this on the init method:
let teste = currentWeather["temperature"] as Int
temperature = convertToCelsius(teste)
but it does not work, it says:
"cannot convert the expression's type() to type Int"
Why is that??
Or someone has another way to do this???
Thank you so much...
1 Answer
Gareth Borcherds
9,372 PointsYour function doesn't allow you to pass a variable to it so you can't pass in then temperature to do your calculation. Let's try this as your function
func convertToCelsius(temperature : Int) -> Int {
let tempToCelsius = ((temperature - 32) * (5/9))
return tempToCelsius
}
That should work, but it's still not returning a value, I think it has something to do with 5/9 and converting to integer. But that should solve your first problem.
Miguel Martin
21,703 PointsMiguel Martin
21,703 Pointsi figured that out but now i have other problem...
i corrected the function and in the init method i did:
temperature = convertToCelsius(currentWeather["temperature"] as Int)and it gets me an error saying:
"variable self.temperature" used before being initialized"
and its there the variable and its being initialized with the value from the function.
i am trying to solve this issue for over 1 hour and still can't get what i am doing wrong