一旦实例化,请尽快做某事

发布于 2025-02-11 03:13:36 字数 1181 浏览 4 评论 0原文

这似乎是一个简单的问题,但是我无法在Swift文档中找到任何解决方案。当我在Kotlin进行编程时,通常使用类的init()方法来执行一些工作,以便进行类设置并准备好使用。

现在,我正在创建一个uiviewRementable,并想在初始化此结构后立即做某事。似乎init()用于结构的方法仅与成员初始化有关。

例如,.onappear view提供此功能如下:

struct MapView: View {
    @StateObject private var viewModel = MapViewModel()
    
    var body: some View {
        Map(coordinateRegion: $viewModel.region,
            interactionModes: .all,
            showsUserLocation: true)
        .onAppear { 
            // this will occur as soon as the View appears
            viewModel.safeCreateLocationManager()
            
        }
    }
}

但是现在,当定义uiviewReprementable struct时,我希望执行相同的:

struct OverlayMapView: UIViewRepresentable {
    @StateObject private var viewModel = MapViewModel()
    
    // immediately do the following: 
    viewModel.safeCreateLocationManager()

    
    func makeUIView(context: Context) -> MKMapView {
        ...
    }
    
    func updateUIView(_ uiView: MKMapView, context: UIViewRepresentableContext<OverlayMapView>) {
        ...
    }
}

This seems like a straightforward question, but I have not been able to find any solutions in the Swift docs. When I was programming in Kotlin, it was common to use the init() method of a class to perform some work in order to get the class set up and ready for use.

Now I am creating a UIViewRepresentable and would like to do something as soon as this struct is initialized. It seems that the init() method for structs is only concerned with member initialization.

For example, the .onAppear method for a View provides this functionality as follows:

struct MapView: View {
    @StateObject private var viewModel = MapViewModel()
    
    var body: some View {
        Map(coordinateRegion: $viewModel.region,
            interactionModes: .all,
            showsUserLocation: true)
        .onAppear { 
            // this will occur as soon as the View appears
            viewModel.safeCreateLocationManager()
            
        }
    }
}

But now when defining a UIViewRepresentable struct I am wishing to do the same:

struct OverlayMapView: UIViewRepresentable {
    @StateObject private var viewModel = MapViewModel()
    
    // immediately do the following: 
    viewModel.safeCreateLocationManager()

    
    func makeUIView(context: Context) -> MKMapView {
        ...
    }
    
    func updateUIView(_ uiView: MKMapView, context: UIViewRepresentableContext<OverlayMapView>) {
        ...
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长途伴 2025-02-18 03:13:36

这是可能的,但是不正确的(因此我甚至不会告诉),而是使用它

struct OverlayMapView: UIViewRepresentable {
    @StateObject private var viewModel = MapViewModel()
    
    func makeUIView(context: Context) -> MKMapView {
       viewModel.safeCreateLocationManager()    // << here !!

       // view creation is next ...

*makeuiview是在init in 之前呼叫 onappear

It is possible but would be not correct (so I will not tell even how), instead use it

struct OverlayMapView: UIViewRepresentable {
    @StateObject private var viewModel = MapViewModel()
    
    func makeUIView(context: Context) -> MKMapView {
       viewModel.safeCreateLocationManager()    // << here !!

       // view creation is next ...

*the makeUIView is called after init and before onAppear

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文