0

I am trying to write an iOS application and get the error in the title. The problem is not with the @State property but with the order of initialization. In my opinion this post (SwiftUI @State var initialization issue) does not answer the question why the above error only occurs if the @State properties are declared before the normal variable. I created an simple example so there is less code to read.

The error occurs in the initializer of this SwiftUI View:

import SwiftUI
import MapKit
import CoreLocation

struct SimpleExample: View {
    @State var trackingMode: MapUserTrackingMode = .follow
    @State var searchRadius = CGFloat(200.0)
    let isWholeScreen: Bool
        
    var body: some View {
        VStack(spacing: 0) {
            Text("Placeholder")
        }
    }
    
    init(trackingMode: MapUserTrackingMode, searchRadius: CGFloat, isWholeScreen: Bool) {
        self.trackingMode = trackingMode
        self.searchRadius = searchRadius
        self.isWholeScreen = isWholeScreen
    }
}

struct SimpleExample_Previews: PreviewProvider {
    static var previews: some View {
        MapView(trackingMode: .follow, searchRadius: CGFloat(200.0), isWholeScreen: true)
    }
}

enter image description here

If you change the order of initializations everything works. I am interested in why it works when the order is reversed

init(trackingMode: MapUserTrackingMode, searchRadius: CGFloat, isWholeScreen: Bool) {
        self.isWholeScreen = isWholeScreen
        self.trackingMode = trackingMode
        self.searchRadius = searchRadius
//        self.isWholeScreen = isWholeScreen
    }

My only idea is that it’s caused by @State.

neldierto
  • 21
  • 6

0 Answers0