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 Methods

Just can't understand how to visually think and implement all the steps.

Hi, I am facing a lot of trouble in Object-Oriented Swift 3, methods. I can't understand what is going on. It will be great help if someone can simplify the video. I feel that I just can't get a grasp on this topic. It really upsets me to know that not understanding these complex things will trouble me later on. Can anyone please recommend something to understand this topic better. I am trying hard to visualize. I can understand that the we have set the points around the tower to one. Rest is all gibberish for me. I would like if someone could just explain how the code is working. Is it just me, or do other people learning Swift 3 have this trouble. Your help is greatly appreciated. Thanks in advance.

5 Answers

So I think what is hard about this tutorial is that we had to learn structs and methods at the same time. If you understand functions, then I imagine that your troubles are with understanding structs. So I would recommend diving deeper into learning just structs. Then try to apply it to a more simpler concept other than gaming, because I don't believe that is the easiest way to introduce Object Oriented.

Often people use Cars, Pets, or People to explain object oriented concepts, because they can reach a larger audience with various experience with Object Oriented.

Here is how I come to understand this material. It's corny, but sometimes applying the concepts to easier silly things can help until you can solidify the concepts.

What is a struct? It takes constants, variables and functions and put them inside a special little isolated box. It puts some initial restrictions on how you can access the little box and do things with it. This is why it is special and known as a blueprint. To tell the difference between the special box 'stuff' and other 'stuff' outside the box (variables, constants, functions) structs stuff is called properties and methods.

What are Methods? Methods are like functions except that they are special. They are in a special box. Otherwise, they can be moved out of the struct, and with some minor adjustments just be a function.

Why are they inside a struct and not in global space? Because only some things can understand this special function and can actually make use of it. If you have a Toaster struct with a function that can toast bread, would you want your Car struct to try to make toast? No. So, it makes sense to move a function that makes toast inside a special box so that only things that can actually toast can use it. What are things that can be toasted? Bread, bagels, muffins can be toast, so you can imagine those things being instances of the Toaster and would used the Toaster's special method 'make toast'.

Why properties and not basic variables and constants? This is the same for methods, a Toast struct would required anything that wants to use it to have certain things. If it allows anything than it wouldn't be so special. So, what would a Toaster struct have? Maybe things such as a type of bread. But a Car struct will a type of engine So again it makes since to takes these types of constants and variables and move them into the special box where they can be understood and used.

Lastly here a basic is an example.

struct Toaster { let breadType: String

func makeToast() {
    print('Here's your toasted \(breadType)!')
}

}

// So I want to be part of the special toast group let myToast = Toaster(breadType: "bagel")

// Yay I feel like I am in the right spot. This special box does some things I need. let myMeal = myToast.makeToast()

So if you can, try to find things in real life that you might can see as being a struct. What type of things that real object has would be it's properties and what type of things that object can do will be it's methods.

Hi, Thanks a lot. I am starting to understand the topic a bit with the example provided by you. So, first let myToast = Toaster(breadType: "bagel") which means that we are choosing the bread and then when we write let myMeal = myToast.makeToast() we mean to toast and write on the console. Is that correct? Can you please explain what that upperBound, lowerBound and x - range and x + range is. Your help means a lot to me. Thanks again. Cheers.

I feel we can use the functions to do the work that the structs are doing. What is so special about them?

You probably can use funcs over structs. As far as if structs are useful, do you find Arrays, Ints and Strings useful? According to the instructor these are structs. If you don't see them as useful, try defining your very own array from scratch or try to find the source code for the array Type. That is a lot of work. Can you think of things you can do with an array that you shouldn't be able to do with a string? If so, then you can see why Arrays, Strings and Ints will have their own special functions. If you wanted to create an add function that add things together, wouldn't you think it would be better to just associate each type of 'adding' with the appropriate type of adding to be done or would you just want to check for every single type of thing to add in your function, handle all the errors etc? I would want to seperate adding arrays together vs adding strings together vs adding numbers together.

Setting an upper and lower bound means that you pose restrictions on how close something can be to you before you attack it or how far away. Say a person you want to kick has to be at least 1 feet away from you because if they are too close you can't really kick them. But then again they can't be more than 3 feet away from you, because then you leg can't reach them. It's the same for the points in the game I think. I think enemies had to be as close as 1 point away because otherwise they will be right on top of you. But at the same time they couldn't be more than 1 point away because the tower gun wouldn't reach them.

Can you give a few more examples of structs. Thanks.

I will try. I am new to structs too. I am more familiar with functions and classes. I'm guessing a struct is somewhere in between the two. Here's a real current example.

Problem: I am working on this one problem where I have to examine a string that gives information about what kind of browser and operating system a user used on their device to access a web page (such as www.teamtreehouse.com).

Original solution use a single functions: The first time I address this problem, I looked through the special type of string and extracted the browser and operating system all in one function. The one function grew to be many lines of code, because several conditions had to be check for finding the browser and then for finding the operating system.

Updated solution: Right now I have two separate functions that use that same type of string to give me that information. One function uses the string to find the device browser and the second function uses the string to find the device operating system. So now each of these functions can nicely do 2 separate tasks with less lines of code.

Updated problem: Now, there is other information in that same very type of string that can possibly even give me the hardware-type of the device used such as a Samsung Galaxy note or iPad. So, I would have to write yet another function to get that information. So those are 3 separate functions all using the same type of string to do their work. If you put these with all the other functions that are doing other type of work in the script, without significant amounts of comments, it all can get mixed together. If someone else wanted to use my script, it wouldn't be so easy. If my special string type changed, I will have to locate every function that used the old special spring type and update it with the new type of string. That can be a headache and error prone because I can easily forget to make the changes.

Next updated solution: So eventually I would probably create a struct called 'deviceInfo' that will take in the special type of string. Then I will put those original 2 functions in that struct as special methods. Then any other function that uses that same very string can easily be put into that struct as a special method. That way they are all together and if I need to make a change to the special string type, I can do it in 1 place, the 'deviceInfo' struct. Also, if someone has that special type of unique string and want to extract device browser, operating system, hardware or anything else possible, they can use my special 'deviceInfo' data type and have access to all of it even when it is updated.

This was a really bad example for a beginner and as a more experienced programmer I get where you are coming from. When I first started object oriented programming I didn't know what instance was. Some programmers refer to objects as instances. Object oriented programming is concerned with the creation of objects and manipulating those objects. Let's say we had a game, the different aspects of the game could be considered objects. For example we could an object to model the hero. This hero would have properties. Methods or functions could be used to manipulate the object. Think of a struct or class (because they are similar) as blue prints to creating objects.

To create an object that would model a hero we could use

struct Hero { //properties let name: String var life: Int let intelligence: Int var armour: Int

//methods

func gainLifeBonus() { life += 50 } }

to create a hero objects and store it in a variable or constant

let hero1 = Hero(name: "Superman", life: 1000, intelligence: 6, armour: 950) let hero2 = Hero(name: "Batman", life: 750, intelligence: 9, armour: 600)

Do you understand functions and how they work?

Yes, very well.