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

Adam Beck
Adam Beck
9,345 Points

A question about 'class'

So i've tried a wide range of answers with no luck. I left my answer 'as is' and wanted to see if anyone knew what I am doing wrong. Having trouble with "self.location" and assigning it with 'struct Location'. Here is the question..

"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."

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

let someBusiness = Business(name:  location: )

2 Answers

Michael Reining
Michael Reining
10,101 Points

Hi Adam,

You are very close. The initializer for the Business is wrong. If a class contains a Type of another class, you only need to link it to the type. The initialization only happens when you create an instance of the class.

See details below.

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 // just link it to parameter name above
    }
}

let someBusiness = Business(name: "Some business",
    location: Location(latitude: 45.5,longitude: 50.0)) // initialize location

I hope that helps,

Mike

PS: If you found the above helpful, be sure to check out my app which breaks code challenges down and really explains things so you can learn faster.

Code! Learn how to program with Swift

https://itunes.apple.com/app/code!-learn-how-to-program/id1032546737?mt=8

PPS: I could not have developed the above app without going through the awesome resources on Team Treehouse. Stick with it. It is so worth it!

Adam Beck
Adam Beck
9,345 Points

Thank you! I will definitely check that out. The Treehouse community has been nothing less than awesome!

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 67,989 Points

There are two errors to fix.

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  // assign value passed to parameter
    }
}

// let someBusiness = Business(name:  location: )
// Provide all attributes to the initializer. Use mock data
let someBusiness = Business(name: "Test", location: Location(latitude: 10, longitude: 20))