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

Chris Miller
Chris Miller
2,186 Points

Stuck again on initializer methods...Challenge 2 someBusiness

I can't see what's wrong or more accurately, I can't see how to fix it. Here is the compiler error message I get.

Playground execution failed: error: test.xcplaygroundpage:10:20: error: use of unresolved identifier 'Business' let someBusiness = Business(name: "craps", location: someLocation) ^~~~~~~~ Please help.

objects.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: "craps", location: Location(latitude: 4.3, longitude: 51.2))

2 Answers

Jeff McDivitt
Jeff McDivitt
23,970 Points

Your class is inside your structure, that is why you are getting the error. Move the class outside the struct and you have it

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 someBusiness = Business(name: "My House", location: Location(latitude: 34.0, longitude: 26.0))
Chris Miller
Chris Miller
2,186 Points

Hmm. Thanks that totally fixed it. Looking back through some of the course examples I'm not sure how missed that classes can't be inside structs. Thanks again.