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

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!