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

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!