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

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.