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 trialBrandon Sherwood
Courses Plus Student 1,090 PointsClasses with Custom Types
Its saying check preview, and when i go to check my preview there isnt anything there! what am i doing wrong?????
struct Location {
let latitude: Double
let longitude: Double
}
class Business {
let name: String
let location: Location
init(name: String, latitude: Double, longitude: Double) {
self.name = name
self.location = Location(latitude: latitude, longitude: longitude)
}
let someBusiness = Business(name: "School", latitude: 34.0, longitude: 32.0)
}
2 Answers
Christin Lepson
24,269 PointsThe question asks you to set up the Business initializer to accept a name of type String, and a location of type Location. In your init method, you accept a String for name, but you use two Doubles instead of one Location. The code should look like this:
struct Location {
let latitude: Double
let longitude: Double
}
class Business {
let name: String
let location: Location
init(name: String, location: Location) {
self.name = name
self.location = location
}
}
let location = Location(latitude: 34.0, longitude: 32.0)
let someBusiness = Business(name: "School", location: location)
Daniel Zoccali
3,132 PointsAnonymouse user: Your solution will work and complete the overall challenge, however, the reason you have less syntax here is because you are in fact hard coding the values of location within your initializer method, thus, the class is redundant because the programmer cannot alter its latitude and longitude values.
Therefore, Christian's solution is more appropriate and flexible.
Brandon Sherwood
Courses Plus Student 1,090 PointsBrandon Sherwood
Courses Plus Student 1,090 PointsThank you! I figured it out on my own but i appreciate the help!
Anonymous User
Courses Plus Student 2,204 PointsAnonymous User
Courses Plus Student 2,204 PointsThere is actually another way to do this using less syntax: