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

Daniel Cohen
Daniel Cohen
5,785 Points

Expected pattern error in initializer

Not sure why I'm getting a "Expected pattern" error here for both the name and location lines in the initializer. Any help would be appreciated.

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


class Business {
    let name: String
    let location: Location

    init(name: String, latitude: Double, longitude: Double) {
        let self.name = name
        let self.location = Location(latitude: latitude, longitude: longitude)
    }
}

let someBusiness = Business(name: "Testing Enterprises", latitude: 1.1, longitude: 5.5)
print(someBusiness)

2 Answers

Hi,

You dont need to create a new constant within the init method, so removing let should solve your problem.

self.name = name
 self.location = Location(latitude: latitude, longitude: longitude)

I'm still learning so I cant give you a great explanation of this, however I believe that you are creating local copies of the constant above when initializing the class.

Hopefully someone will offer us both a better exaplanation, but for the time being hopefully this will help.

Here is the link to the Apple Doc on Initialization.

https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.html

Cheers, Marc

Daniel Cohen
Daniel Cohen
5,785 Points

Of course! Thanks. I don't know why I've used let there - I guess a habit from the previous lessons. I could have stared at it for another few hours and I wouldn't have found that.