Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Object-Oriented Swift Complex Data Structures Adding Instance Methods

rico petrini
rico petrini
522 Points

if you already answered my previous questions dont ask this one becuase i dont want to be a pest. but i dont understand.

i do nut understand calling out the full name and stuff

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

    func fullName() -> String {
      return "\(firstName) \(lastName)"
    }
}
struct Person {
let firstName: String
let lastName: String 
}
let myFullName = Person(firstName: "Rico", lastName: "Petrini")
let aPerson = fullName

1 Answer

Dave Berning
Dave Berning
17,365 Points

Hi Rico.

If you're stuck, I would try using the code below. I'll break down the it down for you.

First, you are defining a struct which is a collection of properties, kind of like an object but structs can contain methods. Next, you create your method fullName() inside your struct that returns a string (first name and last name, concatenated).

Now you need to create an instance of that "Person" struct and pass in the needed arguments for the struct function. In "let aPerson", you are simply creating an instance and defining the arguments for the string that will be returned when you call the function to return it. The reason why you need to create another constant (let myFullName), is because you need to access the method inside of the struct. When you actually access or call that method, then and only then will the string, "Rico Petrini" get returned.

// What the code challenge wants you do to
struct Person {
    let firstName: String
    let lastName: String

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

let aPerson = Person(firstName: "Rico", lastName: "Petrini")
let myFullName = aPerson.fullName()

Hope that helps!