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

Valeria Ruiz
Valeria Ruiz
948 Points

classes

hi guys, I have been working on this exercise for a while now.

Basically it is saying that I haven't identified "location" but I don't know what else to do to identify it.

Can anyone help me find what is missing in this exercise?

Thanks :)

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

    class Business{
    let name: String
    let location: Location

    init(x: Double, y: Double){
    self.name = String ("Houston")
    self.location = Location (latitude: x, longitude: y)
    }
    }
}
let someBusiness = "name, \(location)"

1 Answer

Classes and Structs can get complicated, especially when you are using them together. One thing to remember is that you always have to create an init function for a class whereas you do not need to unless you need to override the default one for a struct. Let's walk through this code together.

struct Location {
    let latitude: Double
    let longitude: Double
// You are need to end the struct definition before you begin the class definition
}

// We start by defining a class named Business
class Business{

    // Great job setting up the properties here
    let name: String
    let location: Location

    // next we need to set up the init function for the class.  We need to pass in parameters for the 2
    // properties that we set up above
    init(name: String, location: Location) { // We expect a name parameter that is a String and a location parameter that is a Location Struct 
    // We then use those arguments to set the properties
    self.name = name // self.name refers to the class property and name refers to the argument passed in
    self.location = location // Same as above
    }
}

// now that we have everything defined we create an instance of the Business class
// as a constant named someBusiness.  We will pass in a string and a Location instance
let someBusiness = Business(name: "MyCo", location: Location(latitude: 100, longitude: 120)) // this is just creating a new Location and passing it in as the location parameter

Hope this clears things up. Let me know if you still have any questions about anything. Have fun learning!!