< Common SwiftUI errors and how to fix them | How to fix “Initializer 'init(_:rowContent:)' requires that ‘SomeType’ conform to 'Identifiable’” > |
Updated for Xcode 14.2
SwiftUI’s views should be structs, which means they are immutable by default. If this were our own code we could mark methods using mutating
to tell Swift they will change values, but we can’t do that in SwiftUI because it uses a computed property.
If you want to change a property’s value while your program runs, you should mark it using @State
, like this:
@State private var runCount = 0
For more complex properties, such as reference types or values that are shared across multiple views, you should use @StateObject
, @ObservedObject
or @EnvironmentObject
instead.
SAVE 50% To celebrate WWDC23, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
Link copied to your pasteboard.