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 trialWesley Jacobs
5,865 PointsInstance Methods
Help, I'm stuck! Maybe I'm just beyond my understanding of swift but I can't seem to grasp the calling of these methods. Am I supposed to create a function INSIDE the struct that clusters the information properly and then return that?
struct Person {
let firstName: String
let lastName: String
}
let fullName = Person(firstName: "wes", lastName: "jacobs")
3 Answers
Andrew Dell
3,203 PointsThank you. Here's my code for the second part:
struct Person {
let firstName: String
let lastName: String
func fullName() -> String {
return "\(firstName) \(lastName)"
}
}
let aPerson = Person(firstName: "Andrew", lastName: "Dell")
let myFullName = aPerson.fullName()
Underneath your answer, I assigned an instance of the Person structure to a constant named aPerson.
Since we can only use these methods on INSTANCES of the class, then I called the fullName method on this new 'aPerson' and assigned it to another variable. I dunno if this helps. I'm sorta confused myself.
Wesley Jacobs
5,865 PointsI GOT IT NVM! hahaha
Andrew Dell
3,203 Pointscare to share your answer?
Wesley Jacobs
5,865 Pointsstruct Person { let firstName: String let lastName: String func fullName() -> String{ return "(firstName) (lastName)"
}
}
This got me past the first part of the quiz but now i'm stuck on the second.
Wesley Jacobs
5,865 PointsWesley Jacobs
5,865 PointsThat's what I got too. I'm still piecing together how that worked. Haha