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 trialTrick Spades
3,182 PointsUILabels not updating for Weather App.
For some reason the simple weather app is not updating the labels. It's just staying as the default Label text that I've set.
ViewController:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var currentTemperatureLabel: UILabel?
@IBOutlet weak var currentHumidityLabel: UILabel?
@IBOutlet weak var currentPrecipitationLabel: UILabel?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
if let plistPath = NSBundle.mainBundle().pathForResource("CurrentWeather", ofType: "plist"),
let weatherDictionary = NSDictionary(contentsOfFile: plistPath),
let currentWeatherDictionary = weatherDictionary["Currently"] as? [String: AnyObject]{
let currentWeather = CurrentWeather(weatherDictionary: currentWeatherDictionary)
currentTemperatureLabel?.text = "\(currentWeather.temperature)ΒΊ"
currentHumidityLabel?.text = "\(currentWeather.humidity)%"
currentPrecipitationLabel?.text = "\(currentWeather.precipProbability)%"
}
}
CurrentWeather Class:
import Foundation
struct CurrentWeather {
let temperature: Int
let humidity: Int
let precipProbability: Int
let summary: String
init(weatherDictionary: [String: AnyObject]) {
temperature = weatherDictionary["temperature"] as! Int
humidity = weatherDictionary["humidity"] as! Int
precipProbability = weatherDictionary["precipProbability"] as! Int
summary = weatherDictionary["summary"] as! String
}
}
I don't understand what I'm doing wrong. Any help would be greatly appreciated.
EDIT: Even after turning the Precipitation Chance & Humidity into Doubles, it still does not update Labels. Temp: 90 | Humidity: 88% | Precip: 88%
3 Answers
agreatdaytocode
24,757 PointsI think I found it.
You have "let currentWeatherDictionary = weatherDictionary["Currently"] as? [String: AnyObject]"
It needs to be "let currentWeatherDictionary = weatherDictionary["currently"] as? [String: AnyObject]"
Difference is c vs C
agreatdaytocode
24,757 PointsTrick,
It's been a few months sense I have looked at this app. I don't remember pulling weather data from a plist. Can you tell me what part of the course you are on so I can take a look.
P.S You can also check out my finished app on github and compare the two. https://github.com/Mav3r1ck/Project-RainMan
Trick Spades
3,182 PointsHey Aaron, Thanks for your reply, you were the first person on the list of people to ask, and I truly appreciate your "swift"ness ;)
The course I'm on is "Build a Weather App With Swift", and my issue occurred during "Simple Data Structures". I thought I followed everything to a point, but for some reason my Labels are not updating.
And I'm 100% sure my outlets are connected to the labels because of the circle in the gutter with the dot inside of it.
I'm going to take a look at your app though. Thanks :)
agreatdaytocode
24,757 PointsTrick,
I'll take a look at the course and let you know what I find.
Trick Spades
3,182 PointsThanks a lot.
Looking at your app, both our CurrentWeather's are identical (besides a few name changes). That's where I thought the issue would be, but they're identical.
Your ViewController is more advanced than mine, but I'm looking at Pasan's code at the part I'm currently on, and it's identical.
If it helps, in the beginning of the lesson, when he tried to run the app he got one of the "green" errors (and his app crashed) that showed up on his AppDelegate, whereas I did not, and my app finished building.
Trick Spades
3,182 PointsTrick Spades
3,182 PointsYou're a legend Aaron. Thanks for your time.
agreatdaytocode
24,757 Pointsagreatdaytocode
24,757 PointsAny time Trick :)