Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Object-Oriented Swift 2.0 Classes Classes with Custom Types

Yong Wha Hong
Yong Wha Hong
1,237 Points

What is wrong with the initializer I wrote?

Why does my posted code not compile?:

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: Double, longitude: Double)
    }

}

let someBusiness = Business(name: "Starbucks", location: Location(latitude: 43.0, longitude: 43.0))

I get that the following initializer works: init(name: String, location: Location) {

    self.name = name
    self.location = location

}

But previous videos also seem to show that initializers can take parameters that are stored values from structs outside of the class.

Any help is much appreciated!

Jake Adams
Jake Adams
1,608 Points

Just so I know we're on the same page, can you share the code that isn't working?

1 Answer

Marc Schultz
Marc Schultz
23,356 Points

The tasks contains the following sentence:

In the initializer method pass in a name and an instance of Location to set up the instance of Business.

This means the init method is only allowed to be like:

init(name: String, location: Location)

Your solution is not really wrong, but the internal test methods of the challenge expect the above constructor, so your solution will not be accepted.