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

Thomas McNish
Thomas McNish
10,893 Points

I'm still confused about initializers and classes

I think I understand Structs fairly well (I'm still a beginner), and I understand the concept of Classes, but I'm confused about the syntax and I'm particularly confused about initializers. It's getting muddy for me when I try to figure out passing in arguments and creating instances. Here's the problem I'm stuck on.

In the editor you've been provided with a struct named Location that models a coordinate point using longitude and latitude values.

For this task we want to create a class named Business. The class contains two constant stored properties: name of type String and location of type Location.

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.

I've gotten some of the basic code in, but I don't know where to go from here. Can anyone help me better understand initializers and methods? Here's what I've gotten so far.

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(latitude: 33.9, longitude: 49.8)
    }
}

Thank you!

3 Answers

Steven Deutsch
Steven Deutsch
21,046 Points

Hey Thomas McNish,

The purpose of an initializer is to prepare an object for use. In order for an object to be ready for use, all of its properties must have values. When we call the initializer method, we pass it the specific arguments that we have specified as its parameters in the initializer declaration. These arguments are used to assign values to the properties of that object.

The problem with your code is this:

You are passing an instance of location as the second argument of the initializer, however, you never use the argument that you pass in. Instead, you are creating a separate instance unrelated to the argument you passed in and are assigning it to the location property.

class Business {
    let name: String
    let location: Location

    // Notice how you use name but location never gets used in the body
    init(name: String, location: Location) {
        self.name = name
        // this is where you create a new instance instead of using location
        self.location = Location(latitude: 33.9, longitude: 49.8)
    }
}

What you want to do is 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
    }
}

// Now when we call this initializer, we pass in an instance of Location as the 2nd argument
// Here I create the instance of Location directly inside the initializer call
let someBusiness = Business(name: "AnyString", location: Location(latitude: 20.0, longitude: 20.0))

Hope this clears a few things up, Good Luck!

Thomas McNish
Thomas McNish
10,893 Points

Thanks Steven, that was very helpful, and I think I understand what you're saying. However, I do have one more question that could use some clarification. There's a line in the instructions that says "In the initializer method pass in a name and an instance of Location to set up the instance of Business."

I thought that we created an instance of Business outside of the class, when we assigned it to the constant someBusiness. How do you pass in an instance (of Location in this example) in an initializer method?

EDIT: Steven Deutsch thank you so much! That was very helpful. I feel a lot more confident about these concepts now. I really appreciate you breaking it down for me like that. I was definitely hitting a wall, and I now feel like I can move forward.

Steven Deutsch
Steven Deutsch
21,046 Points

First off, I had to correct the typo. For some reason I was missing the Business type name before the call to my initializer.

// The code on the left side of the = is the call to the initializer, this creates an instance
// We assign that instance to the constant someBusiness, on the left side of the =
let someBusiness = Business(name: "AnyString", location: Location(latitude: 20.0, longitude: 20.0))

If we take a look at the initializer method for Business, it looks like this:

// You can see it takes two parameters
// The first is name, of type String
// The second is location, of type Location
init(name: String, location: Location) {
        // Those values being passed in are then assigned to the properties here
        self.name = name
        self.location = location
    }

This means when we call the initializer (like any other function), we have to pass it the values for its parameters.

This means we will be passing it two arguments, a string for the name, and an instance of the Location type for the location.

// Here I pass in a string literal for name "AnyString"
// Then I pass in an instance of Location for location
// I created the Location inside of the initializer call to Business by calling the Location init
let someBusiness = Business(name: "AnyString", location: Location(latitude: 20.0, longitude: 20.0))

I could have done it this way as well:

let someLocation = Location(latitude: 20.0, longitude: 20.0)
let someBusiness = Business(name: "AnyString", location: someLocation)

Hope this helps.

Steven Deutsch
Steven Deutsch
21,046 Points

No problem Thomas McNish it's all about breaking down walls. You will face them countless times when learning development. Every week I still face a few that make it seem like it's almost impossible to continue, but eventually things work out.

The initializer in the Business class is close.

class Business {
    let name: String
    let location: Location

    init(name: String, location: Location) {
        self.name = name
        self.location = location // pass in the paramater in the init function
    }
}

Think of the init() method as a way to give stored properties (without default values) values. Later when you "Using this initializer, create an instance and assign it to a constant named someBusiness."

// Create a new constant to hold an instance of Business
let someBusiness = Business(name: "Apple", location: Location(latitude: 4332.0, longitude: -3243.0))
Paul Je
Paul Je
4,435 Points

The object is the class, or struct; for now just know that it's the entity that you're referring to which you can prepare the properties (var or constants) for battle (quite literally) via initializing the properties and naming them (or equating) to the initialized values that you'd want to pass in your end values into. Think of it as an output based on the type of the properties from the properties themselves. Hope this made sense... Good luck!!!