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

Matt Smith
Matt Smith
878 Points

'Call the instance method and assign the full name to a constant named fullName.' - What is my next step and why?

What is my next step in this challenge and why?

structs.swift
struct Person {
    let firstName: String
    let lastName: String
    func getFullName() -> String {
    return "\(self.firstName) \(self.lastName)"
}
}
let aPerson = Person(firstName: "Harry", lastName: "Potter")

let fullName = getFullName() 

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Oh Matt Smith you're so close here! You created the method. You tried to call the method, but a method must be called on an object. You just forgot to say which object! Here's your next step:

let fullName = aPerson.getFullName()

Now the why part... that's a good question. But let's say that we have a database full of names first and last. And maybe we want to write something that will print out envelope labels. Maybe our database lists the last name first and first name last. But we could use this function to get the full name of the person and use it to write our labels. Just one possible scenario :smiley:

Hope this helps! :sparkles:

Matt Smith
Matt Smith
878 Points

Thank you very informative. Why does the return have need to have self. as a prefix to the name strings?

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Self represents the current object. What if you didn't have one person but two?

let aPerson = Person(firstName: "Harry", lastName: "Potter")
let bPerson = Person(firstName: "Darth", lastName: "Vader")

When we run the method on the current object in question, it needs to represent itself... not some other object. So if we do this:

let fullName = bPerson.getFullName();

The name returned will now be Darth Vader. :smiley: