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

Jake White
Jake White
2,108 Points

what am i doing wrong?

I don't understand

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

            class Business {
                let name: String
                var location: Location
                init(name: String, latitude: Location, longitude: Location) {
                    self.name = name
                    self.location = Location(latitude: <#T##Double#>, longitude: <#T##Double#>)
                }
}
let someBusiness = Business(name: "name", latitude: Location, longitude: Location)

2 Answers

Alexander Smith
Alexander Smith
10,476 Points

Should be modeled as such

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: "a business",location: Location(latitude: 2.2, longitude: 1.1))

I'd be happy to answer any questions regarding how any of this works

Ben Harris
Ben Harris
1,683 Points

Hi Alexander, I had a little bit of a problem with the last part regarding how to pass the latitude and longitude. Could you please just explain further why you have to write the location type? Is that because its referring to a structure?

Thanks :)

Alexander Smith
Alexander Smith
10,476 Points

That's exactly correct. Even though a struct doesn't need a custom init method since it has one automatically, you still need to call it like one for implementation purposes. Also since location is of type Location, implementation is necessary.