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

Whats wrong with my code?

Treehouse won't accept this code, is this a bug??

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

    init(latitude: Double, longitude: Double) {
        self.latitude = latitude
        self.longitude = longitude
    }
}

class Business {
    let name: String
    let location: Location

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

let someBusiness = Business(name: "REI", Location(latitude: 12.1, longitude: 12.2))

3 Answers

Ramiro Martinez
Ramiro Martinez
13,858 Points

I think it's giving you an error, because in the class, it is suppose to fill the location constant with the location parameter in the init method, and then when you are going to create the someBusiness object, you need to pass a Location object like an argument. Try 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 locationExample = Location(latitude: 12.1, longitude: 12.2)

let someBusiness = Business(name: "REI", location: locationExample)
Nichanan Kesonpat
Nichanan Kesonpat
2,159 Points

Ramiro is right. Your solution was very close. When you're creating an instance of the Business class, you're already passing in a name (a String) and a location (a Location instance), so the Location instance itself does not have to be created in the init method of Business.

The fix would be to change: self.location = Location(latitude: Double, longitude: Double) to self.location = location

To clarify, the term "location" in the fixed code will be assigned to whatever Location instance you pass in when you're creating someBusiness (which in your case would be Location(latitude: 12.1, longitude: 12.2) ). Does that make sense?

Paul Heneghan
Paul Heneghan
14,380 Points

Thanks, guys, I was stuck with the exact same problem, and tried several variations on that last Location piece. Assigning it to a constant worked for me.