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

Celsius to Fahrenheit UISwitch

I have just finished the make a weather app course and decided to add a UISwitch to change the temperature label reading from Celsius to Fahrenheit and vice versa. Thing is I can't seem to get my code right.

I added an IBOutlet for the switch as follows:

@IBOutlet weak var celciusToFahrenheit: UISwitch!

then my code goes as follows private let forecastAPIKey = "APIKey" // private-access control

       let coordinate: (lat: Double, long: Double) = (37.8267,-122.423) // Tuple


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    retrieveWeatherForecast()

        }


 override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func retrieveWeatherForecast() {
    let forecastService = ForecastService(APIKey: forecastAPIKey)
    forecastService.getForecast(coordinate.lat, long: coordinate.long) {
        (let currently) in

        if let currentWeather = currently {
            // Update UI
            dispatch_async(dispatch_get_main_queue() ) {// Specify main queue in parameter
                //Execute code. part of GCD (dispatch...) To make run on main thread. dispatch function used to make changes in the UI from background queue.

//I start dealing with the switch here
@IBAction func switchPressed(sender: AnyObject) {

                if let temperature = currentWeather.temperature {

                    let celsius = (temperature - 32) * 5 / 9

                    if self.celciusToFahrenheit.on {

                        self.currentTemperatureLabel?.text = "\(celsius)º"

                    } else {

                        self.currentTemperatureLabel?.text = "\(temperature)º"
                    }
                }


                }



                if let humidity = currentWeather.humidity {
                    self.currentHumidityLabel?.text = "\(humidity)%"
                }
                if let precipitation = currentWeather.precipProbability {
                    self.currentPrecipitationLabel?.text = "\(precipitation)%"
                }

                if let icon = currentWeather.icon {
                    self.currentWeatherIcon?.image = icon
                }

                if let summary = currentWeather.summary {
                    self.currentWeatherSummary?.text = summary
                }
                self.toggleRefreshAnimation(false) //stopping toggle
            }

        }
    }
}

@IBAction func refreshWeather() {
    toggleRefreshAnimation(true) //starting toggle
    retrieveWeatherForecast() // To refresh put original online call in a func
}

func toggleRefreshAnimation(on: Bool) {

    refreshButton?.hidden = on
    if on {
        activityIndicator?.startAnimating()

    } else {
        activityIndicator?.stopAnimating()
    }

}

}

I have added all the code from the ViewController for reference in case you have completed the same course.

Thank you in advance!

2 Answers

As far as i understand you should not rely on @IBAction func switchPressed(sender: AnyObject) {...}, this way the app will stuck on waiting until the switch is going to be pressed. You should implement an 'if' statement depending on which side the switch is placed.

I'll try that, thanks!