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

iOS Build a Weather App with Swift (Retired) Displaying Our Weather Data The End!

Chris Daley
Chris Daley
2,998 Points

Hi, 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
PLUS
James Fairhurst
Courses Plus Student 2,377 Points

You 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.

Chris Daley
Chris Daley
2,998 Points

Hi 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!

Thanks! I was going through the documentation but wasn't 100% sure how to use the "si". This worked like a charm!

For 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
Chris Daley
2,998 Points

Thanks Matthew, this is really detail, will have a play around of your code!

Could you please tell me how to implement the swipe handler with the code you written. Thanks in advance.