Swift classes and structures

Hello everyone who can explain me difference between classes and structures, where should I use classes? And I didn’t get the idea of inits or can you guys recommend me a tutorial where I can understand the topics which I mentioned

for initializers watch this video

and this shortly afterwards

a structure and a class is at first glance, quite the same

struc person{
name=“tom”
}

and

class person{
name=“tom”
}

is essentially the same, the main different is that a class can be inherited (Extended) while a struc cannot.

so in our example the person class can be extended to lets say an employee class that inherits from person, this employee will now have a new attribute called “work”

class employee : person {
work = “teacher”
}

so if you use employee it will have both attributes “name” and “work”

other than that another main difference is that structures are “value types” while classes are “reference types”

  • Value type: each instance keeps a unique copy of its data. A copy of an instance is created when it’s assigned to a variable, constant or property.
  • Reference type: each instance shares the same copy of the data. A reference to one instance is created when it’s assigned to a variable, constant or property.

a value type works like this:

a = 1
b = a
a = 3

print b //this will still be 1 even though we changed a afterwards to 3

a reference type works like this:

a = 1
b = a
a = 3

print b // b is now 3 because we changed a into 3

these two differences are essentially what makes classes and structure unique

so if you are looking to use just a simple class/object that you have no intention of extending (or inheriting from) then you can use stuctures if you want