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

Method Quiz

I am really lost here, if someone could show me how to do it and explain why that would be absolutely amazing.

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

1 Answer

Nathan Tallack
Nathan Tallack
22,160 Points

Ok. Here is how your code would look. Pay attention to the fact that the "method" is a function inside of the struct. Also I am using string interpolation to make the full name return string. You could use string concatonation (fullName + " " + lastName) also.

See at the end we create your person object and we call the method on that object instance to get your fullname back from that function. :)

struct Person {
    let firstName: String
    let lastName: String

    func getFullName() -> String {  // Here is our method with our return.
        return "\(firstName) \(lastName)"  // Here is our return.
    }
}

// Now e create an instance of the person passing the parameters in.
let aPerson = Person(firstName: "Israel", lastName: "Bracey")

// Now we call the instance method to get the fullname as a string.
let fullName = aPerson.getFullName()

I hope this helps. :)