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

Pete McPherson
seal-mask
.a{fill-rule:evenodd;}techdegree
Pete McPherson
iOS Development Techdegree Student 5,062 Points

No errors in xcode, compiler error on challenge. Not sure what's wrong!

I'm extremely confused on the 2 different options available with the Class init definition...specifically for this challenge (class with custom types).

Specifically, the code below runs in xcode with no errors, and seems correct in my head. Could someone advise?

objects.swift
struct Location {
    let latitude: Double
    let longitude: Double
}

class Business {
    let name: String
    let location: Location

    init(name: String, latitude: Double, longitude: Double) {
        self.name = name
        self.location = Location(latitude: latitude, longitude: longitude)
    }

   let someBusiness = Business(name: "Pete", latitude: 33.2, longitude: 22.3)
}

1 Answer

Thomas Dobson
Thomas Dobson
7,511 Points

Your code wouldn't compile for me. I think its important to note that you need to pass the initialized location as type Location.

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: "WalMarketPlace", location:Location(latitude: 20, longitude: 45))
Pete McPherson
seal-mask
.a{fill-rule:evenodd;}techdegree
Pete McPherson
iOS Development Techdegree Student 5,062 Points

Thanks Thomas. I had that class set-up originally, but had failed to properly pass through the type Location.

This helped greatly. Thanks!