Friday, July 12, 2024
HomeiOS DevelopmentLet and var in Swift defined – Donny Wals

Let and var in Swift defined – Donny Wals


Nearly each programming language could have some means to outline properties; Swift does too. We now have two approaches to defining a property in Swift. We are able to use a var or a let. The code beneath reveals how we are able to 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.

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 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, after I make an occasion of my Member object, I can change the title as wanted:

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

occasion = Member(title: "Oliver")

We additionally consult with a var as being mutable. That is one other approach of claiming that the worth for this property can change.

A let is the alternative. It is a fixed worth. Because of this as soon as we have assigned a price, we won’t 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(title: "Donny")
occasion.title = "Hi there, 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 = "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 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 a substitute of a class.

I will not develop into the distinction between structs and courses an excessive amount of on this put up, but it surely’s essential to know {that a} class is assigned to a variable(var) or fixed(let) utilizing its handle 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 would not re-assign to our let occasion within the instance above.

Structs then again are usually saved by worth. Because of this 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(title: "Donny")
occasion.title = "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 price to let occasion. After we change the title of our occasion, Swift has to switch 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 = "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 title after which assign it again to var occasion.

We usually like to jot down our code utilizing let as a substitute of var at any time when 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 sensible to outline that property as a var. When doubtful, use let. Then change it to a var if you discover that you just do have a have to replace that particular property in a while.



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments