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

Julien Schmitt
Julien Schmitt
2,334 Points

Cannot declare instance of business; why?

Hi everyone,

Im trying to declare the instance of business to someBusiness but I guess I did something wrong in the initializer.

Any suggestions?

Thanks,

Julien

classes.swift
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()

3 Answers

Jhoan Arango
Jhoan Arango
14,575 Points

Hello Julien:

The Business class has to be outside the struct that the challenged provided you with. Once you have that done, then you may do the instance of Business.

If you can't seem to pass the challenge with what I just told you, let me know and I will help you even further.

Julien Schmitt
Julien Schmitt
2,334 Points

Hi Jhoan,

I just passed it outside. However when I'm assigning Business to the constant, I'm getting an error. I guess location should show latitude and longitude ?

let SomeBusiness = Business(name: "Paris", location: 2.0)

Jhoan Arango
Jhoan Arango
14,575 Points

Julien, that's correct, since in the business initializer you have a parameter that requires a "Location" then you have to pass it an instance of it as an argument.

There are 2 ways you can do it.

let someLocation = Location(latitude: 20,  longitude: 20)

let someBusiness = Business(name: "Walmart", location: someLocation) 

// Or 

let someBusiness = Business(name: "Walmart", location: Location(latitude: 20,  longitude: 20) ) 

Good luck

Julien Schmitt
Julien Schmitt
2,334 Points

Thanks!

Didn't know we could call struct as arguments.