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

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,558 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
    }
}