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

Help with function method

I'm having issues with the exercise. The previous exercise on functions does not go over some of the function specifics that are used in this object lesson so i'm a little confused about how to specify my function. I don't understand how my function should return the name? For instance, in a previous lesson on functions we used print to create the output. Here we are specifying a return type as a custom type, which is necessary for this method?

structs.swift
struct Person {
    let firstName: String
    let lastName: String
    func fullname(firstname: String = "Ed", lastname: String = " Watson") -> [Person] {

    }
}

4 Answers

Hi Edward,

Your method doesn't need to take any parameters as it can access the two member variables directly. It wants to return the representation of the full name, which is of String datatype.

I'd use interpolation inside the method to return "\(firstName) \(lastName)". You could concatenate, adding a space in between, if you prefer.

So, create your method, state no parameters, make it return a string and use the above line inside the method - that should work.

Let me know how you get on.

Steve.

this doesn't work:

struct Person {
    let firstName: String
    let lastName: String
    func fullName(firstname: String, lastname: String) -> String {
          return firstName + " " + lastName
    }
}

[MOD: edited code block - srh]

Hi Edward,

If you get rid of the parameters that you are requiring the function to receive, that will probably work.

So, lose what's inside the parentheses, but leave the parentheses in place. Your method doesn't need to receive firstName or lastName because it is within the class and can see them directly.

The rest looks fine.

Keep me posted how you get on.

Steve.

excellent that works. thanks

No problem!:+1: