1-
Descri
be
the dif
ferenc
es
betwe
en
structs
and
classe
s in
Swift.
When
would
you
choose
one
over
the
other?
Purpose
:
Evaluates
the
candidate'
s grasp of
Swift's
type
system
and their
ability to
choose the
appropriat
e data
structure
based on
value
semantics
vs.
reference
semantics,
among
other
factors.
In Swift,
both
structs
and
classes are
used to
define
properties
and
methods
to add
functionali
ty.
However,
there are
key
differences
in how
they are
used,
which
stem from
Swift's
value type
(struct) ve
rsus
reference
type (class
)
distinction.
Understan
ding these
differences
is crucial
for making
informed
decisions
about
when to
use each
in your
iOS
application
s.
Differen
ces
Between
Structs
and
Classes
1. Value
vs.
Reference
Semantics
:
The
primary
difference
lies in how
they are
stored and
passed
around.
Structs
are value
types,
meaning
that when
you assign
a struct to
a
variable or
pass it to
a function,
a copy of
the struct
is created.
Classes,
on the
other
hand,
are
reference
types.
When you
assign a
class
instance to
a variable
or pass it
to a
function,
you are
passing a
reference
to the
same
instance,
not
creating a
new one.
2.
Inheritanc
e:
Classes
support
inheritanc
e, allowing
one class
to inherit
the
characteris
tics of
another.
This is a
cornerston
e of
object-
oriented
programmi
ng,
facilitating
code reuse
and
polymorph
ism.
Structs do
not
support in
heritance.
3.
Deinitializ
ers:
Classes
can have
deinitialize
rs, which
are called
when an
instance of
the
class is
about to
be
deallocate
d. Structs
do not
have
deinitialize
rs because
their
instances
are
deallocate
d
automatic
ally once
they are
no longer
needed,
without th
e need for
additional
cleanup.