What’s the difference between Structures and Classes in Swift?

Structures and classes are the building blocks of our systems as Swift developers. They are general purpose containers from which we design our applications.

There are many similarities between structures and classes, but also some key differences:

1. Classes can inherit the characteristics of parent classes. Structures however cannot inherit from other structures (though they can conform to protocols, as can classes).

2. Inheritance of classes means they can be type checked and type casted. As structures do not support inheritance, they cannot be type checked or typed casted.

3. Structures have an automatically generated “memberwise initialiser”. Classes do not.

4. Structures are value types, which are passed by copy, whereas classes are reference types, which are passed by reference. 

You can download a playground which explores these differences in code from GitHub.

The fact that structures and classes are so similar, yet have these fundamental differences often leads to confusion over which to choose for a given role or task. 

Subscribe below and we’ll take a look at some scenarios in which you should choose one over the other soon!

Stay in the Loop

Subscribe to tapdev and never miss a post.

Total
0
Shares
Next Post

6 ways to level up your skills as a Junior Swift / iOS Developer