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

Michel Ortega
Michel Ortega
2,279 Points

Arguments wrong?

While doing this OOP exercise I find that something is wrong with my code and I can't I figure it out. Can someone please help me with this one? Im confused about passing an structure (Point) as an argument of the object.

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



class Business {

    let name: String
    let location : Location

    init (name: String, x: Int, y: Int){

        self.name = name
        self.location = Location(latitude: Double(x), longitude: Double(y))
    }
}

let someBusiness = Business(name: "Shampoo", location: Location(x: 12.3, y: 34.5)

3 Answers

Brandon Lenz
Brandon Lenz
7,937 Points

The initializer for Business takes in three arguments (name, x, y). I

When you are creating someBusiness you are passing in (name, location) only.

If you pass in x and y directly like so, it should work:

let someBusiness = Business(name: "Shampoo", x: 12.3, y: 34.5)

The initializer will convert x and y to a location for you.

Alternatively you can change the input of Business to accept (name, location) like so:

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: "Shampoo", location: Location(latitude: 12.3, longitude: 34.5))

This second method looks to be what you are asking for since you mention passing in a structure as an argument.

Michel Ortega
Michel Ortega
2,279 Points

thanks a lot. now it says "extra argument 'y' in call" how can I solve this, so simple yet I'm baffled.

David Papandrew
David Papandrew
8,386 Points

FWIW, you are missing a close parenthesis at the end of the someBusiness declaration.

Should read:

let someBusiness = Business(name: "Shampoo", location: Location(latitude: 12.3, longitude: 34.5)) // <-- final paren was missing
Michel Ortega
Michel Ortega
2,279 Points

GREAT ! Thank you very much .