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 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!