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 trialPoc Hsu
298 PointsPass parameter's datatype not consistent with it's declared
I followed the treehouse lesson,
let currentWeather = Current(weatherDictionary: weatherDictionary)
And don't know why I should use Current(weatherDictionary: weatherDictionary)
to pass compile
But not Current(weatherDictionary: NSDictionary)
Because I think I declared the weatherDictionary as NSDictionary type
1 Answer
Chris Shaw
26,676 PointsHi Poc,
Structs can be slightly confusing as the constructor always uses named parameters which means even though we haven't declared something like #weatherDictionary
we still need to called the parameter name when creating a new Current
struct, so this is how it works visually.
Current(
weatherDictionary: // This is the named parameter in the `init` constructor function
weatherDictionary // This is the `NSDictionary` we're passing to the `Struct`
)
The only time you need to declare a type is when you're creating a new variable for example.
var myVar: String = "Treehouse is awesome!"
// This is the same as
var myVar = "Treehouse is awesome!"
// And because it's already assigned an explicit type we can assign a new value to the variable without re-declaring the type
myVar = "Now it's even more awesome"
Hope that helps.