Just about each programming language can have some means to outline properties; Swift does too. Now we have two approaches to defining a property in Swift. We will use a var
or a let
. The code beneath reveals how we will outline a var
or a let
as a member of a class
:
class Member {
let id: UUID
var identify: String
init(identify: String) {
self.id = UUID()
self.identify = identify
}
}
This class has two properties. One is a let
, the opposite is a var
.
For those who’re coming from a Javascript background you would possibly anticipate that there is a third choice right here; const
. That is not the case in Swift. Swift solely has let
and var
and a let
in Swift may not be what you suppose it’s.
A var
property is a variable. That implies that no matter we assign to the var
can change over time. For instance, after I make an occasion of my Member
object, I can change the identify
as wanted:
var occasion = Member(identify: "Donny")
occasion.identify = "Howdy, world!"
And since I outlined occasion
as a var
, I am even in a position to create a brand new Member
and assign it to my occasion
variable:
var occasion = Member(identify: "Donny")
occasion.identify = "Howdy, world!"
occasion = Member(identify: "Oliver")
We additionally check with a var
as being mutable. That is one other method of claiming that the worth for this property can change.
A let
is the other. It is a fixed worth. Which means as soon as we have assigned a worth, we won’t change it.
For instance, if I outline my occasion
as a let
as an alternative of a var I am now not allowed to assign a brand new worth to occasion
:
// discover how intstance is now outlined as a let
let occasion = Member(identify: "Donny")
occasion.identify = "Howdy, world!"
occasion = Member(identify: "Oliver") // not allowed, occasion is a let
Moreover, as a result of my Member
outlined id
as a let
, I can not change that both:
let occasion = Member(identify: "Donny")
occasion.id = UUID() // not allowed, id is a let
I can, nevertheless nonetheless change the identify:
let occasion = Member(identify: "Donny")
occasion.identify = "Howdy, world!"
That is as a result of altering a property on my class occasion will propagate as a change to let occasion
. The category occasion assigned to let occasion
remains to be the very same one. We simply modified one of many properties.
This modifications after we’d make Member
a struct:
struct Member {
let id: UUID
var identify: String
init(identify: String) {
self.id = UUID()
self.identify = identify
}
}
The properties on Member
are the very same. The one distinction is that we have made Member
a struct
as an alternative of a class
.
I will not broaden into the distinction between structs and courses an excessive amount of on this publish, nevertheless it’s vital to grasp {that a} class is assigned to a variable(var
) or fixed(let
) utilizing its tackle in reminiscence. So as an alternative of storing the precise class worth in our property, we solely retailer the placement of our class occasion. That is why altering a worth on our occasion does not re-assign to our let occasion
within the instance above.
Structs alternatively are typically saved by worth. Which means while you change a property on a struct, Swift must re-assign the brand new worth to the property that is storing your occasion. Let’s have a look at this in motion:
let occasion = Member(identify: "Donny")
occasion.identify = "Howdy, world!" // this isn't allowed as a result of `occasion` is immutable
What’s occurring within the code above is that we have assigned a worth to let occasion
. After we change the identify of our occasion, Swift has to interchange the previous worth of occasion
with a brand new one as a result of it is a struct and structs are saved utilizing their values.
To permit mutating our occasion.identify
, now we have to retailer the occasion as a var
:
var occasion = Member(identify: "Donny")
occasion.identify = "Howdy, world!" // that is allowed as a result of `occasion` is a variable
Now Swift is ready to make a replica of our Member
with the up to date identify after which assign it again to var occasion
.
We typically like to put in writing our code utilizing let
as an alternative of var
each time we will. The less properties we will change, the extra predictable our code turns into, and the less bugs we’ll ship. Nevertheless, a program that by no means modifications any of its properties would not be very fascinating as a result of it’d simply be a static web page. So in these conditions the place you do want the flexibility to re-assign or replace a property it is sensible to outline that property as a var
. When unsure, use let
. Then change it to a var
while you discover that you simply do have a have to replace that particular property afterward.