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

Wesley Jacobs
Wesley Jacobs
5,865 Points

Instance Methods

Help, I'm stuck! Maybe I'm just beyond my understanding of swift but I can't seem to grasp the calling of these methods. Am I supposed to create a function INSIDE the struct that clusters the information properly and then return that?

structs.swift
struct Person {
    let firstName: String
    let lastName: String
}
let fullName = Person(firstName: "wes", lastName: "jacobs")

3 Answers

Thank you. Here's my code for the second part:

struct Person {
    let firstName: String
    let lastName: String

    func fullName() -> String {
        return "\(firstName) \(lastName)"
    }
}
let aPerson = Person(firstName: "Andrew", lastName: "Dell")

let myFullName = aPerson.fullName()

Underneath your answer, I assigned an instance of the Person structure to a constant named aPerson.

Since we can only use these methods on INSTANCES of the class, then I called the fullName method on this new 'aPerson' and assigned it to another variable. I dunno if this helps. I'm sorta confused myself.

Wesley Jacobs
Wesley Jacobs
5,865 Points

That's what I got too. I'm still piecing together how that worked. Haha

care to share your answer?

Wesley Jacobs
Wesley Jacobs
5,865 Points

struct Person { let firstName: String let lastName: String func fullName() -> String{ return "(firstName) (lastName)"

}

}

This got me past the first part of the quiz but now i'm stuck on the second.