Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

@abhishek-nagpure

Reference Types Vs. Value Types


In Swift, classes are reference types, while structs are value types.
This fundamental difference impacts how instances are stored and
accessed in memory.

Classes :
are reference types, meaning that instances of classes share a
single reference to the same piece of data in memory.
This implies that changes made to one instance will affect all other
instances referencing the same data.
They are typically used for modeling entities with complex
relationships, such as objects in an object-oriented design.

Structs :
are value types, meaning that instances of structs are copied when
they are assigned to a new constant or variable, or when they are
passed to a function.
This implies that changes made to one instance will not affect
other.
They are usually employed for lightweight data structures or for
encapsulating small pieces of data.

Let’s understand this with a simple example on the next page..

@abhishek-nagpure
In the above example, we have a class Human.
We created an instance as human1 and assigned it to human2.
We can see whatever changes we are making in human2 being reflected to human1.

Now using a struct instead restricts changes to the respective instance only.

@abhishek-nagpure
Mutability (Changeability)
In Swift, the concept of mutability refers to whether or not an instance's
properties can be modified after it has been created. Understanding
mutability is essential for designing flexible and robust data structures.

Classes :
Properties of class instances are mutable by default. This means you can
change the values of properties even after the instance has been created.
This mutability is due to the reference nature of classes, where multiple
instances can reference the same piece of data in memory, and changes
made to one instance affect all other instances.

For a class instance, even when it is a constant, we can change the name property.

Structs :
Properties of struct instances are immutable by default if the struct
instance is assigned to a constant (let) variable.
However, you can make individual properties of a struct mutable by
declaring them with the var keyword.
Mutability in structs is achieved through copy-on-write behavior,
ensuring that modifications to a struct instance do not affect other
instances.

However, for a struct, this is not allowed.

@abhishek-nagpure
Inheritance
Inheritance is a fundamental concept in object-oriented programming
that allows a class to inherit properties and methods from another class,
known as the superclass or base class. The class that inherits from the
superclass is called the subclass or derived class.

Classes :
Classes support inheritance, meaning that a subclass can inherit from
only one superclass.
Subclasses inherit all properties and methods from their superclass,
including any initializers, instance methods, and computed properties.

Here, we have derived a Student class, from the Human class.


As we can see while creating an instance we are initializing using, the Human class's
initializer and property.

Structs :
Inheritance is not allowed in struct.

Trying to inherit from struct, throws a compile type error.

@abhishek-nagpure
Identity Checking “==”
The process of determining whether two references point to the same
instance in memory.
In Swift, identity checking is crucial when working with reference types,
such as classes, to understand object equality and identity.

Classes :
The identity of two objects can be checked using the identity operator
(===). This operator compares the memory addresses of two object
references.
If two references point to the same instance in memory, the identity
operator returns true. Otherwise, it returns false.

Structs :
Structs have value semantics, meaning that equality is based on the
contents of the value rather than the memory address.
We can compare instances of struct by comparing
their properties.
The == operator is not automatically defined for structs. Therefore, we
need to explicitly define how to compare instances of our custom struct.

@abhishek-nagpure
Deinitializer
Also known as destructors in other languages, are special methods that
automatically run when a class instance is removed from memory.
They allows for cleanup tasks, like releasing resources, before the object
is gone for good.

Classes :
Deinitializers are defined using the deinit keyword, followed by a code
block enclosed in braces.
Called automatically just before an object is deallocated from memory.
Deinitializers are particularly useful for releasing resources that were
allocated during the object's lifetime, such as closing files, freeing
memory, or unregistering from notifications.

Structs :
Because structs are value types and are copied when passed around,
there's no concept of deinitializing an instance of a struct.
Generated initializer
The generated initializer is automatically provided which initializes all the
properties to their default values, enabling instances to be created
without explicitly defining an initializer.

Classes :
Classes, have no generated initializer provided, Swift generates an empty,
default initializer.

Structs :
An option possible only with structs. “Generated init methods” that are
based on properties

@abhishek-nagpure
@abhishek-nagpure

Like if you learn something new,


Follow if you got value.

@abhishek-nagpure

You might also like