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

Ingunn Augdal Fløvig
Ingunn Augdal Fløvig
1,507 Points

Using instance method to combine first and last name into full name for a structure "Person"

I get an error saying "make sure you're adding an instance method to your struct definition", but I'm not quite sure what I should change to accomplish that.

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

    func fullName(firstName: String, lastName: String) -> [Person] {
    var results = [Person]()

    let firstNameOfPerson = firstName
    let lastNameOfPerson = lastName

    let results = ["\(firstNameOfPerson) \(lastNameOfPerson)"]

    return results
}

2 Answers

Magnus Hållberg
Magnus Hållberg
17,232 Points

I think you have been thinking to much on this and made it over complicated for your self. The challenge say to create a method named fullName() that return a string. It doesn't say you need any inputs so the simple way of doing it is to make a string interpolation from the two properties in the struct and return it.

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