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 trialJoe Hernandez
5,699 PointsNetwork connectivity error or nil
I implemented location services in to the app, when I enter in coordinates the app works fine but when I use userLocation it displays NETWORK CONNECTIVTY ERROR or Nil.
Joe Hernandez
5,699 PointscurrentWeather.Swift
// // CurrentWeather.swift // SPWeather // // Created by Joe Hernandez on 3/25/15. // Copyright (c) 2015 Joe Hernandez. All rights reserved. //
import Foundation import UIKit
struct Current { var currentTime: String? var temperature: Int var humidity: Double var precipProbability: Double var summary: String var icon: UIImage? var windSpeed: Int var visibility: Double
init(weatherDictionary: NSDictionary){
//create instance of a weather dictionary called current weather
let currentWeather = weatherDictionary["currently"] as! NSDictionary
//connect current variable to current weather dictionary
visibility = currentWeather["visibility"] as! Double
windSpeed = currentWeather["windSpeed"] as! Int
temperature = currentWeather["temperature"] as! Int
humidity = currentWeather["humidity"] as! Double
precipProbability = currentWeather["precipProbability"] as! Double
summary = currentWeather["summary"] as! String
//declare iconstring and assign to a image
var iconString = currentWeather["icon"] as! String
icon = weatherIconFromString(iconString)
// initialize time to human readable time
let currentTimeIntValue = currentWeather["time"] as! Int
currentTime = dateStringFromUnixtime(currentTimeIntValue)
}
// convert Unix time to human readable time
func dateStringFromUnixtime(unixTime: Int) -> String {
let timeInSeconds = NSTimeInterval(unixTime)
let weatherDate = NSDate(timeIntervalSince1970: timeInSeconds)
//format time to display 2:00PM type format
let dateFormatter = NSDateFormatter()
//formatted here
dateFormatter.timeStyle = .ShortStyle
// return time here in human readable time
return dateFormatter.stringFromDate(weatherDate)
}
func weatherIconFromString(stringIcon: String) -> UIImage {
var imageName: String
switch stringIcon {
case "clear-day":
imageName = "clear-day"
case "clear-night":
imageName = "clear-night"
case "rain":
imageName = "rain"
case "snow":
imageName = "snow"
case "sleet":
imageName = "sleet"
case "wind":
imageName = "wind"
case "fog":
imageName = "fog"
case "cloudy":
imageName = "cloudy"
case "partly-cloudy-day":
imageName = "partly-cloudy"
case "partly-cloudy-night":
imageName = "cloudy-night"
default: imageName = "default"
}
var iconName = UIImage(named: imageName)
return iconName!
}
}
Joe Hernandez
5,699 PointsJoe Hernandez
5,699 PointsviewController.swift file
import UIKit import CoreLocation
class ViewController: UIViewController,CLLocationManagerDelegate { //Location var seenError : Bool = false var locationFixAchieved : Bool = false var locationStatus : NSString = "Not Started" var locationManager: CLLocationManager! var userLocation : String! var userLatitude : Double! var userLongitude : Double!
//Current weather Outlets "Display Objects"
@IBOutlet weak var locationLabel: UILabel! @IBOutlet weak var visibilityLabel: UILabel! @IBOutlet weak var temperatureLabel: UILabel! @IBOutlet weak var iconView: UIImageView! @IBOutlet weak var currentTimeLabel: UILabel! @IBOutlet weak var humidityLabel: UILabel! @IBOutlet weak var precipitationLabel: UILabel! @IBOutlet weak var summaryLabel: UILabel! @IBOutlet weak var windSpeedLabel: UILabel! @IBOutlet weak var refreshButton: UIButton! @IBOutlet weak var refreshActivityIndicator: UIActivityIndicatorView!
else {
//refresh method @IBAction func refresh() { getCurrentWeatherData() refreshButton.hidden = true refreshActivityIndicator.hidden = false refreshActivityIndicator.startAnimating() }
}