Learn iOS SpriteKit with Examples, Code Snippets and Video Tutorials

Swift Basics – Part 7 – Classes and Structs

Swift Basics – Part 7 – Classes and Structs
October 16, 2014 Justin

This video tutorial we’ll look at classes mostly, and a little bit into structs (or structures).  Structures and classes are very similar in Swift, but I’ll focus my attention on classes since you can do a bit more with them, especially as we head toward some game development with Swift and Sprite Kit in the future.


Structs

In the video, we create a very simple struct. If any of you are familiar with CGPoint, this is basically exactly that: a structure that holds two properties, x and y.

After defining the struct, you could then do something like this…

So now you can use the vector variable like this…

Something worth noting with structs (that isn’t the case with classes),  you get what are called member wise initializers, automatically. These let us set all the properties (members) initially when we create a variable or constant from the struct.  So for example, we could have written…

With classes, that would be something we could do using the init statement (which we’ll discuss in the next video).


Classes

Classes are set up similar to structs. For example, we could have created a class instead of a struct using the same exact code above, and just written “class” instead of “struct”

In the video tutorial, we create a hypothetical game character of type MarioBrother, which is meant to imply it could be either brother, Mario or Luigi.  Take a look…

The class has various properties, xp is an Int, hasMultipleLives is a Bool variable (so it is either true or false), imageName is an optional String value (perhaps to be “Mario” or “Luigi”),  and vector uses our own CharacterVector struct.

Notice too we also have a function in the class named makeJump. So after creating an instance of this class, we can not only access / set the properties above, we can also call functions within the class. And of course, those could accept parameters and return values like any other function.

Lets create that instance and toy with some properties. So anywhere outside of the MarioBrother class itself, we can write…

See how simple that is. We just put mario. in front of the properties or functions we want to access. With Swift those properties are accessible to any other class.  If you’ve never programmed before you might have just assumed that was the case, but for those of us Objective C programmers, we are coming into a brave new world of free-wheeling properties. In the past, we would have had to declare the properties that we wanted other classes to be able to access.


 

Property Observers. 

Go back inside of your MarioBrother class for a moment, and add the following code in brackets after creating our optional String…

What I’ve added here are property observers to listen out for changes to the imageName variable, and execute code before and after that change occurs. So for example, when we gave the imageName a value of “Mario”, the code in the willSet brackets ran right before the change was made, and then the code in the didSet brackets ran. Its a little hard to imagine when and why you would need to detect both changes, but having this option is amazing. Even if you just made use of one of the block, you could do something like play an animation sequence of a character dying if a Bool variable named isDead was changed to true.

We’ll continue talking about classes in the next video.

Continue to Part 8

 

Justin Dike is the owner and lead developer at CartoonSmart.com. He's taught hundreds of hours of game development video tutorials, covering everything from coding to art. These days he's working mostly on Swift and Sprite Kit tutorials, which often lead into highly polished iOS / tvOS Starter Kits, which require no programming at all, but are capable of making nearly any type game! Yeah, you read that write. You can also find Justin at the official CartoonSmart Podcast

0 Comments

Leave a reply

Check out our premium Swift and Sprite Kit Tutorial content!

Visit CartoonSmart.com