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

mohamed hassan
mohamed hassan
2,079 Points

I tried to do the challenge by different ways but I am still missing a lot

I think I don't understand the method the correct way but I tried for couple of days to read about it and resolve the issue but still got errors , if someone can explain and help me solve the challenge please

Given the struct below in the editor, we want to add a method that returns the personโ€™s full name. Declare a method named fullName() that returns a string containing the personโ€™s full name. Note: Make sure to allow for a space between the first and last name

structs.swift
struct Person {
    let firstName: String
    let lastName: String
}
func fullName(first: String, second: String ) -> [Person] {
    let first: String = firstName + " "
    let second: String = lastName
    let exactName = first + second
    return [fullName(first: String, second: String)]
}

1 Answer

Hi there,

The func takes no parameters and returns a string. It is a part of the Person struct so move it inside the struct braces.

As it is part of the struct, it has access to the two stored properties. So, inside the method, I'd just put a string together with string interpolation and return that. This string will be the two names interpolated into a string, separated by a space; use \(varName) notation.

I hope that makes sense.

Steve.