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 Instances of Objects

Kanish Chhabra
Kanish Chhabra
2,151 Points

Structures and Functions

In this video, we made a structure to assign 2 different values to a constant. we can even do that with a function by passing parameters and returning a 'tuple' I am confused!

2 Answers

Will Matthews
Will Matthews
8,127 Points

It's fair to be confused at this point. :) Right now, this simple struct is hard to tell apart from a function because there's not much going on in there yet, but it'll become clearer as we make more complex structs.

Remember when Pasan said a function was kind of like the coffee-maker? Well think of a struct kind of like a coffee shop. There are a lot of coffee-makers, staff records, procedures, etc that "belong" to that coffee shop. There are other instances of coffee shops that will have the same coffee-makers and some procedures, but will have some procedures of their own, and totally different staff records.

Objects like structs work the same way as these coffee shops where they can bundle all these related functions, values, etc together for each instance of the object.

But you're right, it's confusing at this point because there's only a single coffee maker in our coffee shop--so it's hard to tell the difference. It's going to get clearer once you get a chance to add more cool stuff to your structs. :)

I never got it at first either but this is object oriented programming. Our goal is to create and manipulate objects. I'll admit the intro to the video was somewhat not necessary but I understand that Pasan was trying to build on previous knowledge just like a good teacher would.

An object and an instance are the same thing. Some programmers call them objects and others call them instances. Let's say that I wanted to create an object of a hero in a game we could use a struct. Our code would be:

struct hero { name: String life: Int armour: Int intelligence: Int }

The struct is the blueprint and we can use that to create heroes. Look below:

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

In the above examples we have two instances or two objects and we have stored them in two constants

At this point try not to get confused with functions and objects. Functions are usually used to manipulate objects.