Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Object-Oriented Swift Complex Data Structures Adding Instance Methods

Brent Caswell
Brent Caswell
5,343 Points

Having trouble with this code challenge

Hey, I’ve added a function to the structure which should be able to concatenate the firstName and lastName strings, but I’m getting an error saying that I haven’t added a function to the structure. Please advice!

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

    func fullName(firstName: String, lastName: String) -> String {
        let results = "\(firstName) \(lastName)"
        return results
    }
}

1 Answer

andren
andren
28,503 Points

The challenge asks you to add a fullName method, but it does not ask you to take add any parameters to the method. The firstName and lastName that the method should concatenate are the ones that already exists within the Person struct. Since the method is also inside that struct it can access them directly without being passed any data.

Like this:

struct Person {
    let firstName: String
    let lastName: String

    func fullName() -> String { // Declare fullName method with no parameters
        let results = "\(firstName) \(lastName)" // Return `firstName` and `lastName` from struct
        return results
    }
}