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

Matthew Ingram
PLUS
Matthew Ingram
Courses Plus Student 4,718 Points

I am very lost

I don't understand this at all. Why call the instance twice?

structs.swift
struct Person 
{


    let firstName: String
    let lastName: String

    func fullName(theName name: String) -> [String]
    {
        var results = [Person]()
        let Person = ("\(firstName) \(lastName)")
        return results 

        let aPerson = Person(firstName: "Callam", lastName: "Ingram")
    }



}

let myFullName = 

2 Answers

Dhanish Gajjar
Dhanish Gajjar
20,185 Points

Hopefully this will clear things out

struct Person {
    let firstName: String
    let lastName: String

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

let aPerson = Person(firstName: "John", lastName: "Appleseed")
let myFullName = aPerson.fullName()
Will DeBernardi
Will DeBernardi
2,763 Points

That last line is happening because to actually utilize the method that you defined within the struct, you need to apply it to an instance, because that method doesnt actually exist out side of the struct. So when you write:

 let myFullName = aPerson.fullname()

You are actually applying the method to the parameters defined from aPerson.