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

How to get an instance of a struct within the initializer

Now it gets really tough with the challenges… Here I stuck with the following code missing a value for location when trying to assign an instance of Business.

What I understand so far: To get an instance of a class the initializer has to set up the properties for this instance. As values for both name and location aren't assigned in the declaration my initializer will need to do this.

The problem is the location property. It's clear that I need to provide a string for the name when calling an instance of Business. But what would be the correct setting for location on this line of code?

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: "Test")

2 Answers

Ah, I was so near…

The last line of code has to be:

let someBusiness = Business(name: "Test", location: Location(latitude: 10, longitude: 20))
Ramiro Martinez
Ramiro Martinez
13,858 Points

To solve this, you need to make an instance of the struct Location and then pass it to the instance of Business, it's the only thing you need to do.

          struct Location {
    let latitude: Double
    let longitude: Double
}

class Business {
    var name: String
    var location: Location
    init(name: String, location: Location) {
        self.name = name
        self.location = location
    }
}

let location = Location(latitude: 99.222, longitude: 100.345)

let someBusiness = Business(name: "Test", location: location)