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

How do you want me to write a method to return an entire struct as a string? Instructions are unclear.

I can write this a number ways, and I don't think the question is supposed to return fully functioning code, but nothing I've tried has worked, even if there are no errors in my code. The question suggests I should be writing out the string ("allow for a space..."), but no way that I've written it is working and I'm not finding any help online since this is a specific challenge.

I've tried changing the return type to 'String' or an array of the structs '[Person]'. I've structured the instance of the struct within the method a number of different ways with no success. I've tried keeping it as simple as possible and inline with the previous lesson, but nothing works.

The error returned doesn't make sense. Even if the code returns no error in the Preview the same error returns in editor shrug I'm at a loss. I'm just lost on what is being asked for here.

Thanks

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

func fullName() -> (Person) {

    let fullName = Person(firstName: "Name ", lastName: "Name2")
    return fullName
}

Also fails:

func fullName() -> String {

let name = Person(firstName: "MyFirstName ", lastName: "MyLastName")

return ("\(name.firstName) \(name.lastName)")

}

Error suggest my method either takes parameters or doesn't return a string, which as far as I understand, neither apply.

7 Answers

Try placing the function inside the struct.

Thanks Michael, it still returns "Make sure your method returns the correct string, including a space between the first and last name". Space is included in the return string, but it seems it still not the expected format.

Have you got something like this?

struct Person {
    let firstName: String
    let lastName: String

    func fullName() -> String { //fill in here }

}

Sorry, for the delay Michael. Here is what I have:

struct Person { let firstName: String let lastName: String

func fullName() -> String {

let name = Person(firstName: "Name ", lastName: "Name2")
return ("\(name.firstName) \(name.lastName)")

} }

I've simplified the return to:

return ("(name)")

This works as code, but still fails the challenge.

Yeah, that would work in xCode. But for the challenge you could just return 'firstName' and 'lastName'. So it'll look something like this -

struct Person {
    let firstName: String
    let lastName: String

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

}

Thank you Michael! That worked! I had attempted something like that, but I the formatting I used for the return as incorrect. I've never used that formatting before. Good to know.