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

Greg Stone
Greg Stone
2,705 Points

I am completely stuck...

My instructions are to declare a method named "getFullName()" to return a String of a name from firstName and lastName. No matter how many times I watch the video, I cannot seem to figure out what I am supposed to do.

Any help would be greatly appreciated!

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

    func getFullName(firstName: String, lastName: String) -> Person {

        var fullName = Person(firstName: "Greg", lastName: "Stone")
        return fullName
    }
}

1 Answer

Alexander Smith
Alexander Smith
10,476 Points

A method is just a function nested inside of an object like a struct or class. Therefore the method is associated with the object itself and can use the object's properties.

struct Person {
    let firstName: String
    let lastName: String

    func getFullName() -> String {
      return "\(firstName) \(lastName)"  
    }
}

When you go to call a method you do it like this in this challenge

let aPerson = Person(firstName: "Greg", lastName: "Stone")
let fullName = aPerson.getFullName()

Id be happy to answer any additional questions