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 trialChris Daley
2,998 PointsHi, how to use the "Options" from the API to change the temperature from fahrenheit to celsius?
I have looked into the forecast io api document, and find there are options that can change the temperature to celsius. I'm not sure how to use the code they provided.
Thanks for your help!
2 Answers
James Fairhurst
Courses Plus Student 2,377 PointsYou could either use the options in the forecast.io call like you mention e.g.
let forecastUrl = NSURL(string: "37.8267,-122.423?units=si", relativeToURL: baseUrl)
However be careful as that also changes lots of other units. The other way would be to create a new method in your Current.swift struct to change just the temperature to degrees using Maths similar to the dateStringFromUnixTime method for getting the current time.
Matthew Stroh
2,287 PointsFor the simple route, you could just do a converstion method. I implemented the Swipe handler. Swipe left and it changes it to Celcius. Swipe right it switches it back.
func celsiusFromFahrenheit(fahrenheit:Int) -> Int {
let c = ((Double(fahrenheit) - 32.0) * 5.0/9.0) // convert to Celsius
return String.localizedStringWithFormat("%.0f",c).toInt()! // properly round. (This is SO much easier in C#)
}
I added a View level property isCelsiusSet
to hold the Value.
In the closure I use a ternary operator to set the temp:
let temperature = inCelsius
? self.celsiusFromFahrenheit(currentWeather.temperature) //true
: currentWeather.temperature // false
(This could also be done in an if/else statement of course)
Obviously, you would need to use temperature
when setting the label.
Finally I modified the getCurrentWeatherData
signatuare to include a default parameter
func getCurrentWeatherData(inCelsius:Bool = false) -> Void{
However, that was before I checked to see if the API supported Metric... so I'll probably go back and try that out. It was a good exercise, anyhow.
Chris Daley
2,998 PointsThanks Matthew, this is really detail, will have a play around of your code!
arnavthecoder
3,453 PointsCould you please tell me how to implement the swipe handler with the code you written. Thanks in advance.
Chris Daley
2,998 PointsChris Daley
2,998 PointsHi James This works like a charm! I will definitely try the other method as Matthew has clearly demonstrated below. Thanks a lot for your help, really appreciate it!
Jonathan Fernandez
8,325 PointsJonathan Fernandez
8,325 PointsBrilliant! : )
elk6
22,916 Pointselk6
22,916 PointsThanks! I was going through the documentation but wasn't 100% sure how to use the "si". This worked like a charm!