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 Complex Data Structures Adding Instance Methods

Object-Oriented Swift 2.0 Help! Hey I'm stuck and can't seem to figure out anyway to solve this issue.

Specifically, whenever I go to add the "fullName" constant, swift gives me an error about initializing the value, even though I am just creating it for the first time.

struct Person {
    let firstName: String
    let lastName: String

    /////////////////////////////////////////
    ////////////////////////////////////////

    func getFullName() -> String {

    let aPerson = Person(firstName: "Albert", lastName: "Einstein")

    let fullName = aPerson.getFullName()

      return "\(firstName) \(lastName)"

    }

}

1 Answer

Jhoan Arango
Jhoan Arango
14,575 Points

Hello:

So far everything looks good. The only thing is that you have to get the method out of the struct, as well as the calling of it. It should pass once you do that.

You are creating an instance of the struct IN the struct, and it has to be outside of it.

Also, a function is called a method, when is INSIDE of a struct or a class.. outside of it, it's just called a function. This way you can differentiate when they want you to create a function inside or outside of a class or struct.

Hope this helps. Good luck.

struct Person {
    let firstName: String
    let lastName: String

    func getFullName() -> String {

    return "\(firstName) \(lastName)"
    }
}

let aPerson = Person(firstName: "John", lastName: "Smith")
let fullName = aPerson.getFullName()

Thanks, Jhoan. That was extremely clear and makes perfect sense! Happy Holidays,

Cheers.