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 trialMelanie Gershman
4,425 PointsObject Oriented Swift, Classes with Custom Types, solved, but why did it work?
I completed this challenge by hardcoding the values for location within the Business class, but when I was just trying to set the type without hardcoding, I was getting this error: error: cannot convert value of type 'Double.Type' to expected argument type 'Double'
self.location = Location(latitude: Double, longitude: Double)
Why did this work: ` [...]
class Business { [...]
init(name: String, location: Location) {
self.name = name
self.location = Location(latitude: 1.00, longitude: 1.00)
}
} `
But not this:
` [...]
class Business { [...]
init(name: String, location: Location) {
self.name = name
self.location = Location(latitude: Double, longitude: Double)
}
} ` Shouldn't a good development technique be to be abstract as much as possible? Also, if I try to initialize an instance of Business, it winds up having the characteristics in the initializer, NOT the ones I'm trying to impose upon creation (i.e. lat and long will be 1.00, not any other Double I enter in).
Thank you!
1 Answer
Steven Parker
231,269 PointsThis is just a guess.
I'm not a Swift programmer, but based on other languages I know, I would expect that second line of the init method to be:
self.location = location
And the specific values would be passed in when you create the instance, right?
And I'd guess that 2nd answer didn't work because it's a syntax error to redefine Location in that context, but the first one got by because it's syntactically valid and the checker didn't notice that the values were rigidly defined inside the method?
Melanie Gershman
4,425 PointsMelanie Gershman
4,425 PointsOH WOW. That was it. I was overthinking it. Thank you!!