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 trialPablo Lahoz
6,337 PointsUILabels not retrieving plist data
So I have compared the code with Pasan's in the download section and checked out similar questions yet can't figure out where i'm going wrong. The UILabels will only show the numbers I put in the attributes inspector.
heres my code: 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)%"
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
//And CurrentWeather.swift
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
}
}
1 Answer
Pablo Lahoz
6,337 PointsYes, sorry I should have posted this before. The code is fine I just had to check some box in the attributes inspector of the main storyboard. Thank you for replying though.
Tal Zion
6,234 PointsTal Zion
6,234 PointsI used your code it works. Check the plist file to see if the values match