Skip to content

Instantly share code, notes, and snippets.

@ingconti
Created April 26, 2023 13:15
Show Gist options
  • Save ingconti/e0a2ceaf243ff2e27ed02144e506d996 to your computer and use it in GitHub Desktop.
Save ingconti/e0a2ceaf243ff2e27ed02144e506d996 to your computer and use it in GitHub Desktop.
Cannot assign to property: 'self' is immutable FIX it!
//
// AllowChangeContentView.swift
// simpleSample
//
// Created by ing.conti on 26/04/23.
//
/* You got: Cannot assign to property: 'self' is immutable
if You write;
struct ContentView: View {
var msg = ""
...
write SO:
@State var msg = "" // CORRECT!
*/
import SwiftUI
struct ContentView: View {
//var msg = "" // WRONG! try to uncomment..
@State var msg = "" // CORRECT!
var body: some View {
VStack {
Text(msg)
Button("Fill me!", action: {
fillMe()
})
}
.padding()
}
func fillMe(){
self.msg = "Hello!!"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment