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 Complex Data Structures Adding Instance Methods

Maxence Roy
Maxence Roy
8,753 Points

I'm a bit stuck here. I'm trying to return the "full name", can someone give me a tip ?

Task: Given the struct below in the editor, we want to add a method that returns the person’s full name. Declare a method named fullName() that returns a string containing the person’s full name. Note: Make sure to allow for a space between the first and last name.

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

    func fullName(firstName: String, lastName: String) ->  fullNameWithSpace {

        let firstName = "Jenny"
        let lastName = "Tremblay"
        let fullNameWithSpace = firstName + " " + lastName

    }
}

2 Answers

struct Person {
    let firstName: String
    let lastName: String

    func fullName() ->  String {

        return firstName + " " + lastName

    }
}

First, you didn't return a value from your function. Second, the challenge didn't ask you to be able to take in parameters. Third, Your return type is invalid. fullNameWithSpace is a variable name, not a type like String or Int or Double. Lastly, you have a fixed value for the variables firstName and lastName. The challenge didn't ask you to have the variable be a fixed value.

The solution code is shown above.

I hope this helps. :grin:

~Alex

Maxence Roy
Maxence Roy
8,753 Points

Thanks a lot Alex ! I'm gonna need to work a bit more at this.

No problem! :wink:

By the way, can you please provide a Best answer? It will mark your question as answered in the forum (community). Thank you very much! ~Alex