(Obvious) Similarities Between Classes And Structures In Swift Programming Language | by Richmond Alake | Towards Data Science

This article aims to showcase the similarities between Classes and Structures, two different object type within the Swift programming language.
You are not required to have substantial experience using Swift to follow the examples and similarities presented in this article.
The contents in this article can be used as a refresher or an introduction to the Class and Structure objects within Swift.
But why even bother with Swift?
Swift is a versatile programming language used for tasks such as iOS app development and machine learning. It can be a natural language to pick up, especially for developers who have experience in languages such as C or Python.
Below is a quick summary of some similarities, followed by an in-depth exploration.
The process of creating a structure or a class within Swift is almost identical.
For the definition of structures, you simply type ‘struct’, a keyword syntax recognized in Swift, followed by the desired name of the structure and then corresponding opening and closing curly braces.
struct NewCustomStructure {  // Properties, methods...}
Classes follow a similar definition pattern to structures. Simply type the recognized keyword ‘class’ and then the desired name of the class and then curly braces.
class NewCustomClass {  // Properties, methods...}
Structures and Classes namings usually follow the same naming pattern, which is the UpperCamelCase pattern (the first letter of each new word is capitalized).
Experienced programmers that are familiar with Object Orientated Programming languages such as Java, Python or JavaScript will be familiar with the concept of creating instances of classes.
For those who aren’t, creating an instance of a class can be described as viewing the class as a blueprint and an instance of the class is an object built from the blueprint(class).
In Swift, you can create an instance of a class and also create an instance of a structure as well.
The creation of class/structure instances follows a similar pattern. We will create new instances of the previously created structure ‘NewCustomStructure’ and the class ‘NewCustomClass’.
Structure instances are created, as shown below:
let structureInstance = NewCustomStructure()
And class instances are created as shown below:
let classInstance = NewCustomClass()
Both classes and structures can have properties defined within them.
Properties can be thought of as distinguishable containers/placeholder of data that are directly connected with the class or structure they are created within. Variables and constants are properties of both classes and structures.
When an instance of a class or structure is created, we can define the data within their properties.
Properties are created in the same manner in both classes and structures.
To illustrate how we can create, define and access properties in both classes and structures, we will be using structures to define a House, and classes to define a Person.
struct House {  var noOfDoors: Int  var noOfWindows: Int  let noOfRooms: Int  let noOfFloors = 2}
Above is a structure definition that represents a house with some obvious properties that can be found in an actual home.
The code snippet below shows an instance of the house structure that is created and initialized with values for the expected properties of the instance.
let smallHouse = House(noOfDoors:2, noOfWindows:3, noOfRooms: 6)print(smallHouse)>> House(noOfDoors: 2, noOfWindows: 3, noOfRooms: 6, noOfFloors: 2)
The defined properties of the struct instance can be accessed using dot notation.
print(smallHouse.noOfDoors)print(smallHouse.noOfWindows)print(smallHouse.noOfRooms)
To illustrate that properties within classes are accessed in similar methods to structures, we will create a class and access its properties using dot notation.
class Person {    var firstName: String    var lastName: String    var age: Intinit(firstName:String, lastName:String, age:Int) {        self.firstName = firstName        self.lastName = lastName        self.age = age    }}let kindPerson = Person(firstName: "James", lastName: "John", age:23)print(kindPerson.firstName)print(kindPerson.lastName)print(kindPerson.age)
This short article covered the main similarities between the two object types, classes and structures.
I’ve also covered the differences between both object types. Information on the key differences is available via the link below.