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

Trouble creating an initializer method

Can't seem to get the last part of this code correct?

classes.swift
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: "Corey", location: Location)

}

1 Answer

Steven Deutsch
Steven Deutsch
21,046 Points

Hey Corey Hellwege,

Your initializer method is perfect! The problem is you trying to create an instance of Business, inside the Business class. To fix this, move it outside of the curly braces. You also have to initialize Business with an instance of Location. You can't just write the type Location as you have it now.

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
    }

}

// Changed your code to add an instance of Location
let someBusiness = Business(name: "Corey", location: Location(latitude: 20.0, longitude: 30.0))

Let me know if you need more help! Good Luck!

Ohad B
Ohad B
4,869 Points

I had the same problem too! The instructions said: "Using this initializer, create an instance and assign it to a constant named someBusiness" So I thought you need to initialize the Business instance inside the init method.