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
Ben Holland
Courses Plus Student 4,062 PointsMy app Data does not refresh but does make network calls?
Please help me i have tried everything here is my 2 code .
import UIKit
class ViewController: UIViewController {
//Replace the string below with your API Key.
private let apiKey = "c35888949fe1669241327912c904199f"
@IBOutlet weak var iconView: UIImageView!
@IBOutlet weak var currentTimeLabel: UILabel!
@IBOutlet weak var temperatureLabel: UILabel!
@IBOutlet weak var humidityLabel: UILabel!
@IBOutlet weak var precipitationLabel: UILabel!
@IBOutlet weak var summaryLabel: UILabel!
@IBOutlet weak var sunsetLabel: UILabel!
@IBOutlet weak var sunriseLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let baseURL = NSURL(string: "https://api.forecast.io/forecast/\(apiKey)/")
let forecastURL = NSURL(string: "51.515611, -0.133184", relativeToURL: baseURL)
let sharedSession = NSURLSession.sharedSession()
let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(forecastURL, completionHandler: { (location: NSURL!, response: NSURLResponse!, error: NSError!) -> Void in
println(response)
if (error == nil) {
let dataObject = NSData(contentsOfURL: location)
let weatherDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(dataObject, options: nil, error: nil) as NSDictionary
let currentWeather = Current(weatherDictionary: weatherDictionary)
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.temperatureLabel.text = "\(currentWeather.temperature)"
self.iconView.image = currentWeather.icon!
self.currentTimeLabel.text = "At \(currentWeather.currentTime!) it is"
self.humidityLabel.text = "\(currentWeather.humidity)"
self.precipitationLabel.text = "\(currentWeather.precipProbability)"
self.summaryLabel.text = "\(currentWeather.summary)"
self.sunriseLabel.text = "\(currentWeather).sunriseTime"
self.sunsetLabel.text = "\(currentWeather).sunsetTime"
})
}
})
downloadTask.resume()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
import Foundation
import UIKit
struct Current {
var currentTime: String?
var sunriseTime: Int
var sunsetTime: Int
var temperature: Int
var humidity: Double
var precipProbability: Double
var summary: String
var icon: UIImage?
init(weatherDictionary: NSDictionary) {
let currentWeather = weatherDictionary["currently"] as NSDictionary
temperature = currentWeather["temperature"] as Int
humidity = currentWeather["humidity"] as Double
precipProbability = currentWeather["precipProbability"] as Double
summary = currentWeather["summary"] as String
sunriseTime = currentWeather["sunriseTime"] as Int
sunsetTime = currentWeather["sunsetTime"] as Int
let currentTimeIntVale = currentWeather["time"] as Int
currentTime = dateStringFromUnixTime(currentTimeIntVale)
let iconString = currentWeather["icon"] as String
icon = weatherIconFromString(iconString)
}
func dateStringFromUnixTime(unixTime: Int) -> String {
let timeInSeconds = NSTimeInterval(unixTime)
let weatherDate = NSDate(timeIntervalSince1970: timeInSeconds)
let dateFormatter = NSDateFormatter()
dateFormatter.timeStyle = .ShortStyle
return dateFormatter.stringFromDate(weatherDate)
}
func weatherIconFromString(stringIcon: String) -> UIImage {
var imageName: String
switch stringIcon {
case "clear-day":
imageName = "Sun-thin"
case "clear-night":
imageName = "Moon-thin"
case "rain":
imageName = "Rain-thin"
case "snow":
imageName = "Snow-thin"
case "sleet":
imageName = "Hail-thin"
case "wind":
imageName = "windy-thin"
case "fog":
imageName = "Haze-thin"
case "cloudy":
imageName = "cloudy"
case "partly-cloudy-day":
imageName = "PartlySunny-thin"
case "partly-cloudy-night":
imageName = "PartlyMoon-thin"
default:
imageName = "AppIcon"
}
var iconName = UIImage(named: imageName)
return iconName
}
}
1 Answer
Andres Oliva
7,810 PointsOk, I don't know if this is the problem or not, but this line of code:
let forecastURL = NSURL(string: "51.515611, -0.133184", relativeToURL: baseURL)
should be:
let forecastURL = NSURL(string: "51.515611,-0.133184", relativeToURL: baseURL)
(no space in between latitude and longitude)
Andres Oliva
7,810 PointsAndres Oliva
7,810 PointsWhat do you mean with "app data"?
I'm not sure if your dictionary isn't getting populated or if the UI is not updating.