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 trialjames bunn
2,377 PointsCannot Assign Value of Int to Type Double
In the Bootstrapping the UI video, we cast a Double(humidityFloat) to an Int(humidity). However, when doing so, I get an error that I cannot assign a value of Int to type Double.
import Foundation
struct CurrentWeather {
let temperature: Int
let humidity: Double
let precipProbability: Int
let summary: String
init(weatherDictionary: [String: AnyObject]) {
temperature = weatherDictionary["temperature"] as! Int
let humidityFloat = weatherDictionary["humidity"] as! Double
humidity = Int(humidityFloat * 100)
precipProbability = weatherDictionary["precipProbability"] as! Int
summary = weatherDictionary["summary"] as! String
}
}
1 Answer
Steven Deutsch
21,046 PointsHey James Bunn,
Inside your CurrentWeather structure you have the humidity property declared as type Double. You need to change it to type Int. You are retrieving the humidity from a dictionary, as a Double, then converting it to an Int type. This is why you're getting the error: "cannot assign a value of Int to type Double."
import Foundation
struct CurrentWeather {
let temperature: Int
let humidity: Int // Needs to be an Int
let precipProbability: Int
let summary: String
init(weatherDictionary: [String: AnyObject]) {
temperature = weatherDictionary["temperature"] as! Int
let humidityFloat = weatherDictionary["humidity"] as! Double // humidtyFloat is a Double
humidity = Int(humidityFloat * 100) // humidty is an Int
precipProbability = weatherDictionary["precipProbability"] as! Int
summary = weatherDictionary["summary"] as! String
}
Good Luck!
james bunn
2,377 Pointsjames bunn
2,377 PointsYep. Overlooked the initial declaration. Thx!