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

Given a struct, create a method that returns a string containing the person’s full name. What am I doing wrong?

My understanding is a method is just a function within a struct. I've created a fullName method.

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

    func fullName(firstName: String, lastName: String) -> String {
        var name = firstName + "" + lastName
    }

    return name
}

Error returned: Make sure you're adding an instance method named fullName() to the struct definition/

3 Answers

Jeff McDivitt
Jeff McDivitt
23,970 Points

Here is the correct answer, you do not need input parameters for the method. That was not specified in the instructions

struct Person {
    let firstName: String
    let lastName: String

    func fullName() -> String{
        return firstName + " " + lastName
    }

}
james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

you are close. they don't want it to take any arguments so take those out, and instead of storing the names, just return them. also in the middle of the names you have an empty string instead of a space " ".

Anthia Tillbury
Anthia Tillbury
3,388 Points

After three days of stubborn perseverance I had to give in to see that I need not have included any arguments in the Method, which really annoyed me as it broke my momentum, making me not want to face programming as I couldn't get past this.

The thing was that I had figured this out a long time before already, subsequently forcing my hand to cheat by checking the answers, which annoyed me further.

My issue is that I expect to use what I learn on a course module and didn't expect to be repeating what I had already done (this is basically declaring variables with a simple function, but combined into an Object) without learning something new: using arguments in a Method (function).

I expect to use what new information I had been absorbing and in this code challenge I learnt nothing new, but it stopped me for three days, I feel cheated myself now. This should have been a more involved coding challenge.