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

Rick Hoen
Rick Hoen
1,257 Points

Adding Instance Methods

I'm not getting this challenge. It keeps saying "Make sure your function does not take any parameters and returns a String". So I did not add any parameters between the brackets but that does not seem to help.

The preview however keeps saying that I've added a non-void value to a void function.

I have no clue how else to code this challenge, because for instance a "for in loop" does not seem applicable here.

Can anyone help me out with this problem?

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

    func fullName() {
    let wholeName: String = "\(firstName), \(lastName)"
    return wholeName
    }
}
Donald Zarraonandia
Donald Zarraonandia
4,434 Points

I see two mistakes in the above code. One the task calls for a space between firstName and lastName not a ,

This will send it back.

Second you need to specify what the function will return. An example of this would say a func needs to return an Int.

You would write func someFunc() -> Int{}

4 Answers

Hi Rick,

It may be you need to specifically state here the return of that function just after the parentheses if you've come across that before?

Apart from that, there appears to be a stray comma in your let wholeName build there?

Jeff McDivitt
Jeff McDivitt
23,970 Points

Hi Rick -

struct Person {
    let firstName: String
    let lastName: String

//For step 1 you want to create the method that takes no arguments but returns a string of first and last Name
    func fullName() -> String {


    return "\(firstName) \(lastName)"
    }
}

//For step 2 you want to call the method and then assign to a constant 
let aPerson = Person(firstName: "Jeff", lastName: "McDivitt")
let myFullName = aPerson.fullName()
Jordan Ward
Jordan Ward
2,395 Points

Hi Jeff, in step one how come you use interpolation. I tried to do this with concatenation but it didn't work. Why is that?

Your func is void but you are returning a string. The function signature should indicate that it returns a string. Make sense?

Jeff McDivitt
Jeff McDivitt
23,970 Points

Hi Jordan - This works with string concatenation as well

struct Person {
    let firstName: String
    let lastName: String

    func fullName() -> String {

    return firstName + " " + lastName

    }
}

let aPerson = Person(firstName: "Jeff", lastName: "McDivitt")
let myFullName = aPerson.fullName()