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 trialBilly Potts
Courses Plus Student 2,344 Pointscan someone tell me how i assign my instance to a constant
and i was wondering if i was doing it right
struct Location {
let latitude: Double
let longitude: Double
}
class Business {
let name: String
let location: Location
init(name: String, location: Location) {
self.name = "House"
self.location = Location(latitude: 48.069, longitude: 22.174)
}
}
let someBusiness = Business(name: "House", latitude: 48.069, longitude: 22.174)
1 Answer
jcorum
71,830 PointsSorry. You are mixing up initializing with creating objects (or instances):
class Business {
let name: String
let location: Location
init(name: String, location: Location) {
self.name = name
self.location = location
}
}
let location = Location(latitude: 48.069, longitude: 22.174)
let someBusiness = Business(name: "House", location: location)
The initializer takes whatever the class is called with and puts those values into the stored properties. You do not hard-code values into initializers. You give them values by calling the class to make a Business object. And when calling the class you pass the initializer values.
Here there's an extra step: you need to create a Location object. It can be done all in one line but I broke it out as it may be less confusing.