Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Object-Oriented Swift 2.0 Complex Data Structures Adding Instance Methods

Jeff Godown
Jeff Godown
1,714 Points

Help please

Need help figuring out why this isn't working. Thanks!

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

    func getFullName() -> String {
      let person = Person(firstName: firstName, lastName: lastName)
      let fullName = "\(person.firstName) \(person.lastName)"
      return fullName
    }
}
let aPerson = Person(firstName: "John", lastName: "Smith")
let fullName = Person.getFullName(aPerson)

2 Answers

Jari Koopman
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Jari Koopman
Python Web Development Techdegree Graduate 29,349 Points

Hi Jeff,

There are a few minor mistakes in your code. At first, the declaration of the constant person in the method is not needed. Second you should use self.firstName and self.lastName in the fullName constant in the method. Third, you should call the method on the instance of person with aPerson.getFullName(). All in all your code should look something like this:

struct Person {
    let firstName: String
    let lastName: String

    func getFullName() -> String {
        let fullName = "\(self.firstName) \(self.lastName)"
        return fullName
    }    
}
let aPerson = Person(firstName: "John", lastName: "Cena")
let fullName = aPerson.getFullName()

I tried this and it worked just fine. Hope this helped!

Regards, Jari

Jeff Godown
Jeff Godown
1,714 Points

Thank you! It's starting to make more sense.