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

mohamed hassan
mohamed hassan
2,079 Points

still confused about object oriented , still confused about what is inside {} of the struct and whats outside

thanks for the MOD that helped me solving the first part of the challenge however I think I still need some help to finish the whole challenge I did that answer but it is still not going through

structs.swift
struct Person {
    let firstName: String
    let lastName: String
    func fullName() -> String {
    let aPerson = [Person]()

    return "\(firstName) \(lastName)"
    let myFullName = aPerson
myFullName.fullName()
    }
}

who was that MOD user anyway? :-|

3 Answers

Hey Mohammad,

This challenge might seem confusing at first, but to be clear, you are way to overthinking this. This challenge just wants you to take the first and last values of the struct and return the full name in a function named fullName(). Here's the answer to the whole code challenge and it's really not too hard.

struct Person {
    let firstName: String
    let lastName: String

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

let aPerson = Person(firstName: "Maria", lastName: "Te")
let myFullName = aPerson.fullName

Hope this will help! :-)

mohamed hassan
mohamed hassan
2,079 Points

it really makes sense now however when I tried doing the same on the challenge page it keeps telling me Bummer! Your code could not be compiled. Please click on "Preview" to view the compiler errors. I have even tried to copy paste it as I may have typed an error I am not sure of what is going on, but thanks anyway

Alex Busu
PLUS
Alex Busu
Courses Plus Student 11,819 Points

Hey hey Mo,

So I think your confusion comes from this playground business. When I first started playgrounds I found it very odd to just type in the text editor and didn't quite grasp what was being run where. But then we got to the scope tutorials and it all made sense.

Stuff that's inside the Person struct is just being defined, it's not actually executed at all. So in the Person struct we write the logic of what we want this Person struct to be able to do.

Outside the struct definition is where the actual logic happens. That's where we create the Person and find out the persons full name.

When I first learned object oriented programming, we used cars as the analogy.

Think of the struct definition as the blueprint for a car. Inside there you have the color, the engine size, as well as methods, where you can start it, accelerate, break.

When you actually create an instance you take that blueprint and you turn it into a physical object.

But anyway, the answer is as follows

struct Person {
    let firstName: String
    let lastName: String

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

let aPerson = Person(firstName: "Alex", lastName: "Busse")
let myFullName = aPerson.fullName
mohamed hassan
mohamed hassan
2,079 Points

it really makes sense now however when I tried doing the same on the challenge page it keeps telling me Bummer! Your code could not be compiled. Please click on "Preview" to view the compiler errors. I have even tried to copy paste it as I may have typed an error I am not sure of what is going on, but thanks anyway it is even now 2 answers the same I tried both but still an error

Sorry, We both had the same answers but I guess you can just skip the code challenge if it keeps on doing that anyway

Too bad, :-(

mohamed hassan
mohamed hassan
2,079 Points

we forgot to add the () after .fullname thats why it is not accepted

Well, yeah. I guess that's the problem!