不能在 属性 初始值设定项中使用实例成员 'service'; 属性 初始值设定项 运行 在 'self' 可用之前

Cannot use instance member 'service' within property initializer; property initializers run before 'self' is available

我正在尝试从 struct gService 获取经度和纬度并将其设置在地图上。 但无法初始化 @State var region,因为错误“无法在 属性 初始化器中使用实例成员 'service';'self' 之前的 属性 初始化器 运行 是service.latitudeservice.longitude

可用”
struct DetailCardView: View {

let service: gService

@State var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: service.latitude, longitude: service.longitude), span: MKCoordinateSpan(latitudeDelta: 0.0033, longitudeDelta: 0.0033))

var body: some View {

Map(coordinateRegion: $region, annotationItems: [MapPoint(coordinates: CLLocationCoordinate2D(latitude: service.latitude, longitude: service.longitude))]) { location in
                    MapAnnotation(coordinate: location.coordinates) {
                        Circle()
                            .stroke(.red, lineWidth: 3)
                            .frame(width: 50, height: 50, alignment: .center)
                    }
                }

我用谷歌搜索了这个问题,但有不同的情况。

基本上,编译器所说的是“我仍在创建您的 DetailCardView 的实例,我还不知道 service 的值是多少,所以我可以”不要在 region".

中使用它

解决方案是将 service 传递给一个常量,该常量将用于初始化 两个 属性。您需要为传递此常量的视图创建初始化程序。

外观如下:

let service: gService


// Rather than service.longitude and service.latitude, use a dummy value, like 0.0
// Recommended this var to be private
@State private var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0), span: MKCoordinateSpan(latitudeDelta: 0.0033, longitudeDelta: 0.0033))

// Here's your initializer
init(service: gService) {
    self.service = service      // Use service to initialise self.service

    // Use service - and NOT self.service - to initialise region
    region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: service.latitude, longitude: service.longitude), span: MKCoordinateSpan(latitudeDelta: 0.0033, longitudeDelta: 0.0033))
}

var body: some View {
...    // The rest of your code

您可以尝试的另一种方法是在视图出现时删除 init() 并设置 region,如下所示:

let service: gService


// Rather than service.longitude and service.latitude, use a dummy value, like 0.0
// Recommended this var to be private
@State private var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0), span: MKCoordinateSpan(latitudeDelta: 0.0033, longitudeDelta: 0.0033))

var body: some View {
    Map(coordinateRegion: $region, annotationItems: [MapPoint(coordinates: CLLocationCoordinate2D(latitude: service.latitude, longitude: service.longitude))]) { location in
                    MapAnnotation(coordinate: location.coordinates) {
                        Circle()
                            .stroke(.red, lineWidth: 3)
                            .frame(width: 50, height: 50, alignment: .center)
                    }
        .onAppear {
            region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: service.latitude, longitude: service.longitude), span: MKCoordinateSpan(latitudeDelta: 0.0033, longitudeDelta: 0.0033))
        }
                }