iOS Developer: From Noob to Ninja in 30 days — Day 3 First steps in Swift Classes and Structs
By: Tony Trejo
Tags: Application Development, iOS, Swift Programming, Class, Struct

In this post I’ll be talking about classes and structs also we will use playgrounds to show you those differences.

First you will to know how use playgrounds, If you want to know how to use Xcode Playgrounds the links below can help you with that.

What is a Swift Class?

In simple words a class is something written by a programmer and defines properties and actions. Those properties and actions will be used when you create an instance of this class, usually called object.

Let’s create a CrytoExchange class.

// MARK: CrytoExchange class class CrytoExchange { // 1 // MARK: Properties let name: String = "CryptoMex" // 2 let city: String = "Mexico" // 2 // MARK: Actions func sell() { // 3 /// Sell action } func buy() { // 3 /// Buy Action } }

That is the class definition, now this is the explanation.

That is the class name, in this case is called CrytoExchange. The CrytoExchange contains two properties name and city. The CrytoExchange contains two method sell and buy.

Note: The MARK comment allows you to group your code and makes it more readable, you can see more about this in the link below.

But how to used, well after you define your class you have to create an instance of that class, let’s do that.

let mexicanExchange = CrytoExchange() // 1 print(mexicanExchange.name) // 2 print(mexicanExchange.city) // 3 mexicanExchange.buy() // 4 mexicanExchange.sell() // 5

The class instance explanation is this.

The instance name is mexicanExchange and is an instance of CrytoExchange type. Call the property name of the class using the dot notation. Call the property city of the class using the dot notation. Call the method buy of the class using the dot notation. Call the method sell of the class using the dot notation.

What is a Swift Struct?

Structures, or structs, the struct allows you to encapsulate related properties and methods. So that means that class and structs are the same, no! they are not the same, for now let’s create the same CrytoExchange struct.

// MARK: CrytoExchange struct struct CrytoExchange { // MARK: Properties let name: String = "CryptoMex" let city: String = "Mexico" // MARK: Actions func sell() {

/// Sell action

} func buy() {

/// Buy Action

} }

That is the struct definition, now this is the explanation.

That is the struct name, in this case is called CrytoExchange. The CrytoExchange contains two properties name and city. The CrytoExchange contains two method sell and buy.

So, now let’s create the instance of that struct.

let mexicanExchange = CrytoExchange() // 1 print(mexicanExchange.name) // 2 print(mexicanExchange.city) // 3 mexicanExchange.buy() // 4 mexicanExchange.sell() // 5

As you can see it seems the same but as I told you they are not the same.

Differences between Class and Struct

1. The class can inheritance from another class the struct can not inheritance.

The class supports inheritance.

The struct doesn’t support inheritance.

The class can not inheritance from a struct.

Note: The class in Swift does’t support multiple inheritances.

2. The init method (constructor or initializer) has different behavior in a class that a struct.

But in our current example it seems the same, let’s change the example we are going to remove the default value to the properties in the class and the struct, like I show you below.

// MARK: Properties let name: String let city: String

After that change you can see these errors in the class.

Class CrytoExchange has no initializers

CrytoExchange cannot be constructed because it has no accessible initializers

That means you need to write an initializer to be able to use that class, let’s fix that, add the code below in the class below the properties.

init(name: String, city: String) { self.name = name self.city = city }

The to fix the instance creation you need to change the initialization too.

let mexicanExchange = CrytoExchange(name: "CryptoMex", city: "Mexico")

Now the class is fixed, now let’s do the same with the struct, you will see just one error.

To fix that error in the struct instance you just need to replace that initializer.

let mexicanExchange = CrytoExchange(name: "CryptoMex", city: "Mexico")

So after this example we know that a class needs an init method if has at least one property without a predefined value, this also applies when you are using the ! (force unwrap operator).

In the another case a struct will create automatically the init method, so you don’t need to write it.

3. A class instance is reference type and a struct instance is value type.

Note: There are some special cases where a struct can has a reference type behavior.

Reference Type means that will share the same object to any variable that (same memory location).

Value Type means that always would be a copy of that object (different memory location).

You can learn more about this in the link below.

To apply this concept in our example let’s do this changes in both.

// MARK: Properties var name: String var city: String

But you need to know the next thing, if you try to change the value in a struct and your instance is let (constant) you will need to change to var (variable), if you don’t do that you will have these errors.

Cannot assign to property: ‘usaExchange’ is a ‘let’ constant

Let’s change the usaExchange to var like this.

var usaExchange = mexicanExchange

With that change the structure can mutate his properties, that means that when you change something you will create a new copy with the latest value set (value type).

By running the example you can see that after modifying the name and city properties of usaExchange the mexicanExchange still has his own values.

Let’s do the same with the class example.

The are two things to see here:

A class can be let (constant) and his properties var (variable) this is because a class is a reference type.

and his properties this is because a class is a reference type. When you set another property pointing to other instance class they are sharing the same memory location (reference type), that means if you change something in any of those that change will be visible for both.

4. The convenience init

Here is a good explanation of convenience init.

5. The mutable function

Here is a good explanation of mutating functions.

Resources

How to become an iOS developer in 30 days — Day 4 First steps in Swift (protocols in classes, structs and enums)

Structures and Classes Official documentation

Using let or var in Swift

Commenting code in Swift

How to call methods in swift

How to use properties in Swift

I think is enough for this post, I’ll be working on the next post don’t forget to follow me and if you are interested in personal training let me know.

Steve Jobs: “Your time is limited, so don’t waste it living someone else’s life.”