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

NATHAN TRAN
NATHAN TRAN
2,367 Points

Is my struct and constant correct?

Where do the constants aPerson and myFullName go?

structs.swift
struct Person {
    let firstName: String
    let lastName: String

    func fullName() -> String {

    return "\(firstName) \(lastName)"

    }


}

struct Person {

     let aPerson = String

        let myFullName = "\(firstName) \(lastName)"

        print(aPerson(myFullName))





}
Christopher Jr Riley
Christopher Jr Riley
35,874 Points

Yes, the code you posted below should be correct.

1 Answer

Christopher Jr Riley
Christopher Jr Riley
35,874 Points

Your first struct is correct. However, the second struct isn't. As a matter of fact, it's not needed at all.

What the challenge is asking for is for you to assign the instance of Person to the constant that's named aPerson. It would look similar to this:

let aPerson = Person(firstName: "John", lastName: "Doe")

After that, it's asking you to call out the function that you just made (fullName()) and assign it to the constant called myFullName:

let myFullName = aPerson.fullName()

For reference, here's the full code:

struct Person {
    let firstName: String
    let lastName: String

    func fullName() -> String {
        return "\(firstName) \(lastName)"
    }
}

let aPerson = Person(firstName: "John", lastName: "Doe")
let myFullName = aPerson.fullName()

Hope this helps.

NATHAN TRAN
NATHAN TRAN
2,367 Points

this is the full code?:

struct Person { let firstName: String let lastName: String

func fullName() -> String {

    return "\(firstName) \(lastName)"

}

let aPerson = Person(firstName: "Nick", lastName: "Young")

let myFullName = aPerson.fullName()

}