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

isaac brakha
isaac brakha
6,781 Points

Code Challenge Object Oriented Swift problem?

Hello this is the objective, when i put my code it says use of unresolved identifier location. 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(latitude: Double, longitude: Double) {
      self.location = Location(latitude: latitude, longitude: longitude)
    }
}

let someBusiness = Business(name: "isaac", location(2.0, 2.0))

1 Answer

Steven Deutsch
Steven Deutsch
21,046 Points

Hey isaac brakha,

The first problem is that you're not initializing the name property of the Business class. You need to add a parameter name of type String to your initializer.

The next thing we need to change is how you are creating the instance of Business. You are currently trying to pass in a location, however, your initializer doesn't take any argument named location. The way you have wrote it, it takes in a latitude and longitude. It then uses these values to create the Location instance inside the initializer body. Therefore, we need to pass three arguments into our initializer:

  1. a name of type String
  2. a latitude of type Double
  3. a longitude of type Double
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: "isaac", latitude: 20.0, longitude: 20.0)

Good Luck!

isaac brakha
isaac brakha
6,781 Points

Thanks a lot Steven. Very thorough answer. I understand it now..!