如何在 Swift UI 中初始化状态变量而不会出现错误“无法在属性初始化程序中使用实例成员 'XXX';" [英] How can I initialize a State Var in Swift UI without getting the error "Cannot use instance member 'XXX' within property initializer;"

查看:34
本文介绍了如何在 Swift UI 中初始化状态变量而不会出现错误“无法在属性初始化程序中使用实例成员 'XXX';"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望将用户位置放在 MapView 上,并在用户移动时更新位置.我使用@observedObject Var 作为处理位置管理器内容的类,使用@state Var 作为 MKCoordinateRegion() 类.但是,我遇到了一个问题,如果我在声明和定义 @state var 之前放置了 @observedObject var 的声明,我就会遇到错误

I am looking to put a users location on a MapView and have the location update as the user moves. I am using an @observedObject Var for the class handling the location manager stuff and a @state Var for the MKCoordinateRegion() Class. However I am running into an issue where if I put the declaration for the @observedObject var before declaring and defining the @state var I come to the error

(不能在属性初始值设定项中使用实例成员‘纬度’;属性初始值设定项在‘自我’可用之前运行")

我看到其他人发布了这个问题,我尝试了 .init() 函数以及使用惰性变量.然而,问题来了,@state var 需要是 MKCoordinateRegion() 类型,因此需要用值初始化.我不了解如何解决这个问题的根源很可能是不了解如何加载视图的时间表,因为我只使用过 UIBuilder 而不是 SwiftUI.任何帮助将不胜感激!

I have seen other people post this issue and I have tried an .init() function as well as using a lazy var. However the issue comes in to play that the @state var needs to be of type MKCoordinateRegion() and hence needs to be initialized with values. My root for not understanding how to fix this is most likely not understanding the timeline for how views are loaded since ive only ever worked with UIBuilder and not SwiftUI. Any help would be appreciated!

struct MapView: View {
    @ObservedObject var usersLocation = LocationManager()
    var latitude: Double{ return((usersLocation.location?.coordinate.latitude ?? 0))}
    var longitude: Double{ return((usersLocation.location?.coordinate.longitude ?? 0))}
    @State var region = MKCoordinateRegion(
        center: CLLocationCoordinate2D(latitude: latitude , longitude: longitude),
        span: MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1)
    )
    var body: some View {
        Map(coordinateRegion: $region)
            .onAppear()
        Text(String(latitude));
    }
    
}

推荐答案

问题是属性的值是相互依赖的,你不能用这种方式声明它们.

The problem is that the values of the properties depend on each other and you cannot declare them this way.

但是你可以实现一个init方法

But you can implement an init method

struct MapView: View {
    @ObservedObject var usersLocation : LocationManager
    var latitude: Double
    var longitude: Double
    @State var region : MKCoordinateRegion

    init(manager : LocationManager) {
        self.usersLocation = manager
        let lat = manager.location?.coordinate.latitude ?? 0
        self.latitude = lat
        let long = manager.location?.coordinate.latitude ?? 0
        self.longitude = long
        let reg = MKCoordinateRegion(
            center: CLLocationCoordinate2D(latitude: lat , longitude: long),
            span: MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1)
        )
        self._region = State<MKCoordinateRegion>(initialValue: reg)
    }

    var body: some View {
        Map(coordinateRegion: $region)
            .onAppear()
        Text(String(latitude));
    }
    
}

并在父视图中传递 LocationManager 并将其声明为 @StateObject

and pass the LocationManager in the parent view and declare it there as @StateObject

@StateObject var locationManager = LocationManager()
...
MapView(manager: locationManager)

这篇关于如何在 Swift UI 中初始化状态变量而不会出现错误“无法在属性初始化程序中使用实例成员 'XXX';"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆