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 Classes in Swift Classes with Custom Types

I don't understand the objective, Classes With Custom Types, Please help

This is what I have so far

objects.swift
struct Location {
  let latitude: Double
  let longitude: Double
}

class Business {
    let name: String
    let location: Location

    init() {
      let someBusiness = Business(name: "Code", location: 1.1, 2.2)
    }
}

and the thing of the project I don't understand is this. - In the initializer method pass in a name and an instance of Location to set up the instance of Business. Using this initializer, create an instance and assign it to a constant named someBusiness.

1 Answer

David Papandrew
David Papandrew
8,386 Points

Here is how the code would look. Note that the initializer takes two arguments, name and location, and then uses those arguments to set the initial values of the class properties (self.name and self.location).

The constant, someBusiness, gets declared outside the class definition. You create an instance of Business object and pass the values you want for name and location (location takes a latitude and longitude value).

Here is the code:

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(name: "ABC Store", location: Location(latitude: 10, longitude: 10))

Thanks!