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
Scott Lind
2,524 PointsError when running simple weather app from Pasan
Hello,
I get an error when running my 'Simple Weather App' - the iOS course with Pasan.
Xcode gives me a green breakpoint and I can just continue with the execution just fine, but when I load onto my iPhone, it's just stuck.
My code (I've marked the line of code where it gives me the error):
import Foundation
import UIKit
struct Current {
** var currentTime: String? **
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
let currentTimeInValue = currentWeather["time"] as Int
currentTime = dateStringFromUnixTime(currentTimeInValue)
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 = "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 iconImage = UIImage(named: imageName)
return iconImage
}
}
If anyone has experienced the same; please let me know! Any help is much appreciated, thanks!
- Scott
2 Answers
Vittorio Somaschini
33,371 PointsHello Scott.
What error does it return? I think this is not going to be easy to find as it is 3 swift files and a storyboard.
Can you disclose what error it returns? The line you have marked is surely ok like this but this does not means much. Was it the first time you ran your app on the device?
Jorge A. Espinoza
5,675 PointsI've same problem. This is my error "Value of optional type '' 'UIImage' not unwrapped: did you mean to use '!' or '?' ? "
I try this
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!
}
But the app is crash when try to load the icon
fatal error: unexpectedly found nil while unwrapping an Optional value