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

Wojtek Bialy
Wojtek Bialy
867 Points

I don't know how to do this challenge

func fullName(first: String, last: String) -> String { let fullName = "(first) (last)" return fullName }

That and similar versions don't work and I don't know why The Playground shows that everything's OK

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

func fullName() {
    return "\(firstName) \(lastName)"
}
Erik Perez
Erik Perez
5,410 Points

The challenge wants you to add the fullName method to the Person struct. The solution is shown below. Hope it helps!

struct Person {
    let firstName: String
    let lastName: String

    func fullName() {
    return "\(firstName) \(lastName)"
    }
}
Theo VOGLIMACCI
Theo VOGLIMACCI
8,027 Points

Hello, as Erik Perez said here is the proper code to complete the step 1.

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

but don't forget to specify the return type of this func. (type String)

Wojtek Bialy
Wojtek Bialy
867 Points

Thanks a lot guys!

1 Answer

As Theo correctly noted, the issue with your code passing the first step is that the fullName function doesn't specify the return type, which should be String. The second step of the challenge asks you to assign an instance of the Person struct to a constant named aPerson, then use that instance to assign an actual String value of firstName and lastName combined (using the values passed into the aPerson instance) to a constant named myFullName. Below is the complete solution for steps 1 and 2.

struct Person {
    let firstName: String
    let lastName: String

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

let aPerson = Person(firstName: "John", lastName: "Smith")
let myFullName = aPerson.fullName()