Let and var in Swift defined – Donny Wals


Nearly each programming language can have some means to outline properties; Swift does too. We’ve two approaches to defining a property in Swift. We are able to use a var or a let. The code under 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.

Should you’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 won’t be what you assume it’s.

A var property is a variable. That implies 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 = "Hi there, 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 = "Hi there, world!"

occasion = Member(identify: "Oliver")

We additionally discuss 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 as soon as we have assigned a worth, we won’t change it.

For instance, if I outline my occasion as a let as a substitute 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 = "Hi there, 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 = "Hi there, 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 modifications once 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 broaden into the distinction between structs and courses an excessive amount of on this publish, nevertheless it’s necessary to grasp {that a} class is assigned to a variable(var) or fixed(let) utilizing its tackle in reminiscence. So as a substitute 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 would not re-assign to our let occasion within the instance above.

Structs alternatively are typically saved by worth. Which means if you change a property on a struct, Swift should 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 = "Hi there, 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 worth to let occasion. Once 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 = "Hi there, 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 jot down our code utilizing let as a substitute of var at any time when 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 attention-grabbing 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 unsure, use let. Then change it to a var if you discover that you simply do have a must replace that particular property afterward.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles