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

My code doesn't work

Hi, I think I'm doing this right, but I keep getting the error that my code cannot be compiled. Am I calling the instance method correctly?

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

    func fullName() -> String {

    let aPerson = Person(firstName: "Fredrick", lastName: "Barrett")

    }

    let myFullName = Person.fullName()
}

2 Answers

Hi Frederick,

Your fullName method needs to return the full name of the instance. Inside the method, interpolate both member constants into one string, separated by a space.

Then, outside of the class, create aPerson, as you have done. Then call fullName on aPerson and assign the result of that into myFullName.

Make sense?

Steve.

still having trouble with it.

Hi Frederick,

The func puts the two names together and returns them. I'll use interpolation for this but you could use concatenation, with a space.

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

That sits inside the class. Outside the class, create a new instance of Person. You've done this already:

let aPerson = Person(firstName: "Fredrick", lastName: "Barrett")

Lastly, call the func on aPerson:

let myFullName = aPerson.fullName()

I hope that helps,

Steve.

Thank you