Nearly each programming language could have some means to outline properties; Swift does too. We have now two approaches to defining a property in Swift. We will use a var
or a let
. The code under exhibits how we are able to 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
.
Should you’re coming from a Javascript background you may count on 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 signifies that no matter we assign to the var
can change over time. For instance, once I make an occasion of my Member
object, I can change the identify
as wanted:
var occasion = Member(identify: "Donny")
occasion.identify = "Hiya, world!"
And since I outlined occasion
as a var
, I am even capable of create a brand new Member
and assign it to my occasion
variable:
var occasion = Member(identify: "Donny")
occasion.identify = "Hiya, world!"
occasion = Member(identify: "Oliver")
We additionally consult with a var
as being mutable. That is one other manner of claiming that the worth for this property can change.
A let
is the alternative. It is a fixed worth. Which means that as soon as we have assigned a price, we will not change it.
For instance, if I outline my occasion
as a let
as a substitute of a var I am 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 = "Hiya, world!"
occasion = Member(identify: "Oliver") // not allowed, occasion is a let
Moreover, as a result of my Member
outlined id
as a let
, I am unable to change that both:
let occasion = Member(identify: "Donny")
occasion.id = UUID() // not allowed, id is a let
I can, nonetheless nonetheless change the identify:
let occasion = Member(identify: "Donny")
occasion.identify = "Hiya, 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
continues to be the very same one. We simply modified one of many properties.
This adjustments 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 a substitute of a class
.
I will not increase into the distinction between structs and lessons an excessive amount of on this put up, but it surely’s necessary to grasp {that a} class is assigned to a variable(var
) or fixed(let
) utilizing its deal with in reminiscence. So as a substitute of storing the precise class worth in our property, we solely retailer the situation of our class occasion. That is why altering a price on our occasion does not re-assign to our let occasion
within the instance above.
Structs alternatively are usually saved by worth. Which means that 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 examine this in motion:
let occasion = Member(identify: "Donny")
occasion.identify = "Hiya, world!" // this isn't allowed as a result of `occasion` is immutable
What’s taking place within the code above is that we have assigned a price to let occasion
. After we change the identify of our occasion, Swift has to interchange the outdated 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
, we’ve to retailer the occasion as a var
:
var occasion = Member(identify: "Donny")
occasion.identify = "Hiya, 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 usually like to write down our code utilizing let
as a substitute of var
every time we are able to. The less properties we are able to change, the extra predictable our code turns into, and the less bugs we’ll ship. Nevertheless, a program that by no means adjustments 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 power to re-assign or replace a property it is smart to outline that property as a var
. When doubtful, use let
. Then change it to a var
while you discover that you just do have a have to replace that particular property afterward.