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 trial

iOS Object-Oriented Swift 2.0 Classes Classes with Custom Types

Ben Masel
Ben Masel
2,004 Points

I need help on this code challenge, can u help?

I have been trying to look at previous vids and see if that would help but i still can't compile! I need to be steered in the right direction. Thnx!

classes.swift
struct Location {
    let latitude: Double
    let longitude: Double
}

class Business {
  let name: String
  let location: Location

  let someBusiness = init(place: Location) {
    self.latitude = latitude
    self.longitude = longitude
}

1 Answer

Marie Nipper
Marie Nipper
22,638 Points

Pretty close! First: The initialization method should include both the name and location. Not just location. The part where you are adding the "self." within the init method is referencing the properties of the class. Not the struct above. The location property on the class already knows to use the lat and long since you told it to be of type "Location".

init(name: String, location: Location) {
self.name = name
self.location = location
}

Second: Your initialization of the "someBusiness" constant will be outside of your class declaration. The part you're missing is needing to pass the location as an argument.

let locale = Location(latitude: 123.32, longitude: 123.32)
let someBusiness = Business(name: "Apple", location: locale)

All of it together:

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 locale = Location(latitude: 123.32, longitude: 123.32)
let someBusiness = Business(name: "Apple", location: locale)