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

Classes with Custom Types

Its saying check preview, and when i go to check my preview there isnt anything there! what am i doing wrong?????

classes.swift
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: latitude, longitude: longitude)
    }
let someBusiness = Business(name: "School", latitude: 34.0, longitude: 32.0)
}

2 Answers

Christin Lepson
Christin Lepson
24,269 Points

The question asks you to set up the Business initializer to accept a name of type String, and a location of type Location. In your init method, you accept a String for name, but you use two Doubles instead of one Location. The code should look like 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 location = Location(latitude: 34.0, longitude: 32.0)
let someBusiness = Business(name: "School", location: location)

Thank you! I figured it out on my own but i appreciate the help!

There is actually another way to do this using less syntax:

struct Location {
    let latitude: Double
    let longitude: Double
}
class Business {
    let name: String
    let location: Location

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

let someBusiness = Business(name: "Office Block")
Daniel Zoccali
Daniel Zoccali
3,132 Points

Anonymouse user: Your solution will work and complete the overall challenge, however, the reason you have less syntax here is because you are in fact hard coding the values of location within your initializer method, thus, the class is redundant because the programmer cannot alter its latitude and longitude values.

Therefore, Christian's solution is more appropriate and flexible.