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

Having trouble with the method concept

Will someone please explain what I am doing wrong here? I am having trouble understanding why I cannot just return the simple constant fullName.

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


    func getFullName() {
        let fullName: String = "firstName" + " " + "lastName"
        return fullName
    }
}

1 Answer

Nathan Tallack
Nathan Tallack
22,160 Points

So, some shortcuts and some changes. Comments inline.

struct Person {
    let firstName: String
    let lastName: String

    func getFullName() -> String {  // Here we declare that we are returning a String
        // No need to declare the fullName constant here if we are just using it for return
        return "\(firstName) \(lastName)"  // Returning an interpolated string.
    }
}

Thank you. I was overthinking it as usual.