Tutorial – Swift Classes Introduction
Today we are going to look at classes and how to use them in Swift. In this post we will be making a Pet class that has the properties Pet Name, and Number Of Legs the Pet has. From this we will then make a Dog Subclass that inherits the properties from the parent Pet class.
To start off create a new Single View Application XCode Project. Once done we are going to create a new blank Swift Class. Go to File–>New–>File and select “Swift File”. Name this class Pet.
You will now have Pet.swift that is empty except for import Foundation which you can delete as you don’t need it. To declare a new class we use Class <classname> {}. So for pet we do the following to declare it:
class Pet { // Class code goes here }
Now we have the class setup we are going to add properties to it. The properties associate values with a specific class. This allow us to store values in the class such as strings, integers, arrays and so on. In our pet class we are going to declare two properties – the pet name and how many legs it has. To do this we do the following:
class Pet { var name = String() var legs = Int() }
When declaring properties in a class you must either declare it’s datatype (In our case String and Int) or give it a default value from which Swift will automatically determine the datatype. For example we could have done var legs = 2 and Swift would have determined the type as Int() automatically.
Now we have the properties setup to set their values we need to use an initializer to do this add the following to our Pet class:
class Pet { var name = String() var legs = Int() init (name: String, legs: Int) { self.name = name self.legs = legs } }
init is a special method that is used to setup the class when you make a new instance of it. In our Pet Class it takes the argument name and legs, then uses these to set the class properties with self. So for example if we were declaring a new instance of our class we would simply use:
var myPetCat = Pet(name: “Tom”, legs: 4)
This would set myPetCat to have a name of Tom and Legs as 4. We can then later access using just myPetCat.
Now we have the class initialiser and properties setup it is time to add some methods! We are going add three methods to our pet class to do the following:
- Say the pet’s name
- Say how many legs the pet has
- Remove a leg from the pet 🙁
Add the following three methods under what we have already done for our Pet class:
func sayMyName() { println("Hey my name is \(name)") } func countLegs() { println("I have \(legs) legs") } func removeLeg() { self.legs-- }
Congratulations – you have now finished a Class in Swift! Now open ViewController.swift and we will add in some code to see the class in action.
Add the following code in ViewController, in the viewDidLoad() {}
var myPetCat = Pet(name: "Tom", legs: 4) // Setup a new Pet class instance, stored in myPetCat myPetCat.sayMyName() myPetCat.countLegs() myPetCat.removeLeg() myPetCat.countLegs() myPetCat.name = "Tommy" // This is bad practice - you should have a method to set the name myPetCat.sayMyName()
Run the code and you should see the following console output:
Hey my name is Tom I have 4 legs I have 3 legs Hey my name is Tommy
You should be able to follow what each line of code is doing easily, after setting up the class. Note that it is bad practice to set a class variable directly – in the line we have myPetCat.name = “Tommy” we are doing this. Instead it is safer to have a method to set the name in the Pet class as follows:
class Pet { func changeName(newName: String) { self.name = newName } }
And to change the name we use:
myPetCat.changeName("Tommy")
In part of classes you will find out how to add a subclass of Pet, PetDog and see how it works.