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 Instances of Classes

Reagen Rahardjo
Reagen Rahardjo
3,770 Points

please review my code

Hi,

please review my code i got stuck with the terms. from my point of view after declaring a class and create the property inside and also declaring the value. I need to code Init and inside the init I have to assign the NumberOfSides to someShape.

can anyone please tell me where I did wrong

Thank You

classes.swift
// Enter your code below
class shape {
    var numberOfSides: Int
    let someShape: String

    init (numberOfSides: Int){
        self.numberOfSides = someShape
    }
}

3 Answers

Hi Reaghen,

You create a Location object with Location(longitude: 53.1, latitude: 3.1). Substitute that into the parameter for the Business initializer:

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

I hope that helps,

Steve.

Hi Reagen,

You've got most of that right, just not quite in the right place. One point to note; class names are capitalised, so amend that the Shape.

Next, someShape is an instance of Shape, not part of it. Take that line below the class. The init method needs to set the member variable numberOfSides. For that, you could pass an Int into the init method as a parameter, as you have done, then set self.numberOfSides to be equal to that parameter, rather than someShape?

Have a read through that and see if you can figure out the issue with your code' look at the errors in the compiler too. That should all get you through the challenge. As I say, you've got most of it done.

Let me know how you get on.

Steve.

Reagen Rahardjo
Reagen Rahardjo
3,770 Points

Hi Steve,

First i want to say thank you for helping me I already complete the challenge I realize instance has to be called outside the class. I have another question

in this task i already did everything correctly but how do I declare Location Type inside of the someBusiness Instance?

These are the assignment 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.

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

    }
}

//I assume when the location called it should be a set of latitude and longitude type double to be filled inside the instance
let someBusiness = Business (name: "test", location: 12 , 5) //error

let someBusiness = Business (name: "test", location: 12.0 , 5.0) //error

let someBusiness = Business (name: "test", location: "12.0 , 5.0") //error

let someBusiness = Business (name: "test", location: 12.0 5.0) //error

I also tried this method

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(latitude: 12.0, longitude: 13.0) //

    }
}

let someBusiness = Business(name: "ahay", location: Location)