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 having some trouble with the object oriented swift

Can someone explain the logic behind a struct for me I have watched the videos a few times and don't seem to get it? Also, how can I create this method I am totally lost

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


func fullName(wholeName name: firstName + " " + lastName){

return fullName 

}



}
Dennis Witnauer
Dennis Witnauer
12,336 Points

A struct allows you to make an exact copy of data as opposed to a (class) reference to the data. But, I wouldn't get caught up in that. I just kept working through the videos - and practiced - then it clicked. As for the method: The func doesn't take any variables, but remember, in Swift you specify the return type (in this case String). Then, the body of the function returns the two constants in the Person definition using String interpolation. Hope this helps.

struct Person {
    let firstName: String
    let lastName: String

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