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

Laurence Bowe
Laurence Bowe
6,222 Points

Swift weather app Fahrenheit to Celsius?

Hey, Was just wondering whether anyone had solved this so us Aussies can understand it better.

Should probably change the hey to G,Day mate! :) (:

THANKS HEAPS IN ADVANCE!!!

2 Answers

Brad Van Skyhawk
Brad Van Skyhawk
28,489 Points

C is Celsius and F is Fahrenheit. You would take the temperature from forecast.io (which is in Fahrenheit) and then substitute into the formula. Something like this:

You make temperature optional with ? in the declaration

add this in the init function:

    let fahrenheit = currentWeather["temperature"] as Int
    temperature = convertToCelsius(fahrenheit)

add this function to convert it:

func convertToCelsius(fahrenheit: Int) -> Int {
    return Int(5.0 / 9.0 * (Double(fahrenheit) - 32.0))
}
Brad Van Skyhawk
Brad Van Skyhawk
28,489 Points

It looks like forecast.io only return the temperature in Fahrenheit. You can convert in the Current struct with this formula:

C = 5/9 * (F-32)

Laurence Bowe
Laurence Bowe
6,222 Points

WOW, THANKS SO MUCH FOR HELPING OUT AND GIVING UP SOME OF YOUR TIME!!! MEANS AND HELPS ALOT!!! Though how do you implement this in the code, what is C and F? THANKS AGAIN