Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS

james bunn
james bunn
2,377 Points

Cannot 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
Steven Deutsch
21,046 Points

Hey 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
james bunn
2,377 Points

Yep. Overlooked the initial declaration. Thx!