Let and var in Swift defined – Donny Wals


Just about each programming language can have some means to outline properties; Swift does too. We now have two approaches to defining a property in Swift. We will use a var or a let. The code under exhibits how we will outline a var or a let as a member of a class:

class Member {
  let id: UUID
  var title: String

  init(title: String) {
    self.id = UUID()
    self.title = title
  }
}

This class has two properties. One is a let, the opposite is a var.

When you’re coming from a Javascript background you may count on that there is a third possibility 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 title as wanted:

var occasion = Member(title: "Donny")
occasion.title = "Hey, 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(title: "Donny")
occasion.title = "Hey, world!"

occasion = Member(title: "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 worth, we will not 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(title: "Donny")
occasion.title = "Hey, world!"

occasion = Member(title: "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(title: "Donny")
occasion.id = UUID() // not allowed, id is a let

I can, nonetheless nonetheless change the title:

let occasion = Member(title: "Donny")
occasion.title = "Hey, 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 adjustments after we’d make Member a struct:

struct Member {
  let id: UUID
  var title: String

  init(title: String) {
    self.id = UUID()
    self.title = title
  }
}

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 develop into the distinction between structs and lessons an excessive amount of on this put up, but it surely’s necessary to know {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 situation 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 then again 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 have a look at this in motion:

let occasion = Member(title: "Donny")
occasion.title = "Hey, 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 title 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.title, we’ve got to retailer the occasion as a var:

var occasion = Member(title: "Donny")
occasion.title = "Hey, world!" // that is allowed as a result of `occasion` is a variable

Now Swift is ready to make a duplicate of our Member with the up to date title after which assign it again to var occasion.

We usually like to write down 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 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 sensible to outline that property as a var. When unsure, use let. Then change it to a var while you discover that you just do have a must replace that particular property in a while.

Recent Articles

Related Stories

Leave A Reply

Please enter your comment!
Please enter your name here

Stay on op - Ge the daily news in your inbox