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 trialmohamed hassan
2,079 Pointsstill 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
struct Person {
let firstName: String
let lastName: String
func fullName() -> String {
let aPerson = [Person]()
return "\(firstName) \(lastName)"
let myFullName = aPerson
myFullName.fullName()
}
}
3 Answers
Maria Te
8,521 PointsHey 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
2,079 Pointsit 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
Courses Plus Student 11,819 PointsHey 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
2,079 Pointsit 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
Maria Te
8,521 PointsSorry, We both had the same answers but I guess you can just skip the code challenge if it keeps on doing that anyway
Maria Te
8,521 PointsToo bad, :-(
mohamed hassan
2,079 Pointswe forgot to add the () after .fullname thats why it is not accepted
Maria Te
8,521 PointsWell, yeah. I guess that's the problem!
Maria Te
8,521 PointsMaria Te
8,521 Pointswho was that MOD user anyway? :-|