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 trialRyan Maneo
4,342 PointsSo many hypothetical / non visual explanations... can someone please explain this to me as if I was a 4 year old?
I've gotten far past this course, but I've found myself coming back to this exact video, because I know it is the exact point where I got completely lost. I get parts of it, but the way all of the code is being used is foreign to me, and honestly not very well explained. I didn't know For-In loops could go inside of methods and inside of other for-in loops? That was knowledge omitted and frustratingly never recognized until now. I appreciate any answers, and look forward to reading them.
Peter Michiels
3,501 PointsHi Ryan
Could you pls copy-paste some of the exact code you have questions about? I'll be glad to help you out if I can.
Gr Peter
Ryan Oakes
9,787 PointsMethods are essentially struct-specific functions that allow us to do more with our structs. So, instead of a struct just containing simple data points (age: Int, name: String), methods allow us to massage and do work on that data.
So, if methods are struct-specific functions, you can basically do anything in them that you could do in a function! Loops, loops within loops, etc.
Let's say we want to create some objects for a few people and have each one provide a basic greeting. We can do that by creating a struct called Person. We can pass in the properties (name, age) of the person when we create an instance of it, and we can then utilize the greeting method to provide a personalized greeting for each person object.
struct Person {
let name: String
let age: Int
func greeting() -> String{
return("Hello, my name is \(name) and I'm \(age)")
}
}
let ryan = Person(name: "Ryan", age: 30)
ryan.greeting()
"Hello, my name is Ryan and I'm 30!"
let jill = Person(name: "Jill", age: 22)
jill.greeting()
"Hello, my name is Jill and I'm 22!"
By calling the greeting method on our objects, we're able to do something meaningful to them. Don't worry, OOP can be pretty daunting at first, but the pieces will fall in to place eventually - keep practicing!
3 Answers
eitan hershko
1,556 PointsThank you Ryan, it helped me alot
Ryan Maneo
4,342 PointsThank you Ryan!
Anderson Cahet
iOS Development Techdegree Student 2,601 PointsThank you Ryan, it helped a lot!!!!!
Steven Parker
231,275 PointsSteven Parker
231,275 PointsTo a 4-year old I might say: "These funny words are how you tell a computer what to do."
I couldn't resist — but I'm not an iOS programmer so I'll leave the real explanation to one of them.