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 trialAntony Gabagul
9,351 PointsDon't know how to finish the code
Hello. I have slight issues with my code. Unfortunately I don't know how to finish this task. If anyone could elaborate easily how to do it I'd be thankful.
Thank you
struct Person {
let firstName: String
let lastName: String
func getFullName() -> String {
return "\(firstName) \(lastName)"
}
let aPerson = // Don't know what to put here
}
let fullName = //Don't know what to put here
1 Answer
Jennifer Nordell
Treehouse TeacherOk first of all, your creation of the person should happen outside of the struct of the person. Currently you're trying to create a person within the definition of a person. Here's my end code. Let's see if I can explain it step by step.
struct Person {
let firstName: String
let lastName: String
func getFullName() -> String{
return "\(firstName) \(lastName)"
}
}
let aPerson = Person(firstName: "Jane", lastName: "Doe")
let fullName = aPerson.getFullName()
First we have the struct of Person with a variable of firstName of type String and lastName of type String. Those are given to us in the problem. They want us to add a method which essentially gets those two names and creates a string of them after being placed together. So instead of "Jane" and "Doe" we'll have a string of "Jane Doe".
So we declare a function inside the struct which makes it a method. Keep in mind that method is a fancy way of saying a function linked directly to an object. At the end of the day, it's still a function. That function returns a String which is what is asked of us and described above.
Now outside of our struct of a person we create a person (she's born or just joined the company or whatever). And we initialze her with the name "Jane" and lastName "Doe". Then to get her fullName we take that person and run the getFullName on her and store the resulting String in a new variable called fullName.
Hope that makes things clearer! Happy coding!