TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: What is meant by "self is immutable" in this example?

Forums > SwiftUI

I am tring to get a get a user to enter a integer in a TextField and then have the ability for them to add this integer to an array. A simple code example is below.

struct ContentView: View {
    var array: [Int] = []
    @State private var numberString = ""

    var body: some View {
        let numberInt = Int(numberString) ?? 0
        TextField("Enter new number here", text: $numberString)
            .onSubmit {
                array.append(numberInt)
                numberString = ""
            }
    }
}

However, I get an error for array.append(numberInt)saying that Cannot use mutating member on immutable value: 'self' is immutable. I would be very grateful is someone could explain where my misunderstanding lies, and a possible solution to the example above. Many thanks!

2      

Kai faces a common problem:

Error: Cannot use mutating member on immutable value: 'self' is immutable.
I would be very grateful is someone could explain where my misunderstanding lies

Think of a struct as a box.

You put variables and functions inside the box.

But a struct box is made of cement. Once the box is created, built, and on screen, you cannot change it. It is hard cement. It is immutable.

If you want a different version of the box, SwiftUI destroys the old version and creates a new, modified version for you. This seems wasteful, but SwiftUI is very efficient.

But what about the data inside the cement box?
The array of integers? How do we keep track of that array and add new values to it?
In Swift, you store those values OUTSIDE of the cement box. But you keep a reference inside the box. A simple way to do this is with @State variables.

You tell your cement box to build itself, but to use the data that is stored in an external @State variable, you can then change the variable adding new values as necessary.

Try this in Playgrounds:

import SwiftUI
import PlaygroundSupport

struct CementBoxView: View {
    // Stored outside of this box.
    @State private var boxParts: [Int] = []
    @State private var numberString = ""

    var body: some View {
        VStack {
            TextField("Part", text: $numberString)
                .onSubmit {
                    boxParts.append( Int(numberString) ?? 0 )
                    numberString = ""
                }
            Spacer()
            ForEach( boxParts, id: \.self) { Text("\($0)") }
        }.frame(height: 300)
    }
}

PlaygroundPage.current.setLiveView( CementBoxView() )

2      

Hey, thanks for taking the time to leave an explanation I could understand, and for solving my issue. Thanks!

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.