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

I do not get the task?

I don't get the location stuff.

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

2 Answers

Hi Liam,

There's a few parts to this challenge.

First you need to create a new class called Business - that has two stored properties; a name (of type String) and a location (of type Location). The Location struct is provided for us. |Both stored properties are constant, so they're declared with let not var.So, let's write the class:

class Business{
  let name:String
  let location: Location
}

The class needs an init method. This takes two parameters, the Location object and the String. These go into the init method:

init(name: String, location: Location){
  // todo
}

Inside init we set the properties of the class to the parameters passed in, so the name variable inside the class (self) is assigned the value held by the parameter that was just passed in:

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

Put the init method inside the class and that's the class complete. We now need to create an instance of it called someBusiness. That's a constant too, so declared with let. We then assign a Business object to that, and pass the parameters in, as defined in the init method. That needs a name and a Location. A Location object needs creating, though. That holds two stored properties, a longitude and a latitude - both are of type Double so they can handle decimal points; not like an integer. We can create that like this:

Location(latitude: 53.2, longitude: 3.1)

That's one parameter sorted. The other is pretty simple, as it is a string. The whole init method header could look like:

Business(name: "A Business", location: Location(latitude: 53.1, longitude: 3.1))

That needs assigning to someBusiness:

let someBusiness = Business(name: "A Business", location: Location(latitude: 53.1, longitude: 3.1))

Put that lot together, and you're done!

Let me know how you get on.

Steve.

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
        Location(latitude: 53.2, longitude: 3.1)
            }
let someBusiness = Business(name: "A Business", location: Location(latitude: 53.1, longitude: 3.1)
}

This is my code however treehouse says there are compiler errors but Xcode runs it fine??

Hi Liam,

You need to create the instance of the class outside its definition. Also, you've declared a Location object inside the constructor(init) that's doing nothing - you can delete that. And there's a bracket missing at the end of your instance-creation. That does throw an error in Xcode.

So, you need to clean up your init method (remove the redundant Location object), then move your someBusiness declaration outside of the class and add a bracket to it.

That should do it.

Steve.

ok thanks a lot Steve!!!! :)

No problem - let me know how you get on. :+1:

The error output from your code isn't very illuminating, it has to be said!

swift_lint.swift:17:99: error: expected ',' separator
let someBusiness = Business(name: "A Business", location: Location(latitude: 53.1, longitude: 3.1)
                                                                                                  ^
                                                                                                  ,
swift_lint.swift:18:1: error: expected expression in list of expressions
}
^
swift_lint.swift:17:99: error: expected ',' separator
let someBusiness = Business(name: "A Business", location: Location(latitude: 53.1, longitude: 3.1)
                                                                                                  ^
                                                                                                  ,
swift_lint.swift:18:1: error: expected ')' in expression list
}
^
swift_lint.swift:17:28: note: to match this opening '('
let someBusiness = Business(name: "A Business", location: Location(latitude: 53.1, longitude: 3.1)
                           ^
swift_lint.swift:15:9: warning: result of initializer is unused
        Location(latitude: 53.2, longitude: 3.1)

Only two are of any use - the one that says you've not closed the bracket and the one that says your Location object is unused.