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 trialGeovanny Reyes
3,876 PointsCould I get help with this code? I would appreciate it
I don't know exactly how to build the initializer.
struct Location {
let latitude: Double
let longitude: Double
class Business {
let name: String
let location: Location
init(latitude: Double, longitude: Double) {
self.name = "Geo"
self.location = Location(latitude: 2.0, longitude: 3.5)
}
}
}
let someBusiness = Location(latitude: 2.0, longitude: 3.5)
3 Answers
Jennifer Nordell
Treehouse TeacherOk there are a few issues here. First, your class definition shouldn't be defined inside your struct. Your struct doesn't end before the Class definition begins. Secondly the init is going to take a type of String and a type of Location, which you just created. Third, we're not going to hard-code the initial values of the class. Instead we're going to send the values that we want to initialize at the time we create the instance of our Business. So in the creation of the business we will send in a string for the name, and a Location with the latitude and longitude that we specify. Take a look at my solution (which is really just your code edited) to see how we do 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 someBusiness = Business(name: "Geo",location: Location(latitude: 2.0, longitude: 3.5))
Geovanny Reyes
3,876 PointsThank you Jennifer for your help! I hope you don't mind if I ask two more questions... 1. so the class is always outside the struct? {} ... 2. when we declare a init, we don't put any values on it, meaning, like I did when I wrote "Geo" and the numbers? thank you very much!
Jennifer Nordell
Treehouse TeacherAs I understand it you can place a class definition inside a struct. But I've not yet run into a case where this is necessary. There might be instances when you will want to hard-code the init values of some class, but again I can't think of any. Keep in mind though that I'm also a student! :) Hint: I've yet to run into a challenge where either one of those are required, but you may make it further than I have!
Geovanny Reyes
3,876 PointsThat helps a lot! Gracias!
Jennifer Nordell
Treehouse TeacherJennifer Nordell
Treehouse TeacherEdited for typos. Apparently I can't type today :)