获取错误消息“委托必须回复位置管理器:didfailwitherror:''尝试使用Swift在iOS上获得GPS坐标时

发布于 2025-02-09 15:50:07 字数 1624 浏览 3 评论 0原文

在尝试使用(Swift,SwiftUi)在以下教程上获得iOS上的GPS坐标时,我会收到以下错误消息:

错误消息

Thread 1: "Delegate must respond to locationManager:didFailWithError:"

教程的URL

https://www.hackingwithswift.com/quick-start/swiftui/how-to-read-the-users-location-using-locationbutton

我的问题是:

  • 教程不完整吗?
  • 我是否必须添加一些其他代码才能尝试此应用?
  • 错误消息是什么意思?

代码(取自教程)是:

import SwiftUI

import CoreLocation
import CoreLocationUI

class LocationManager: NSObject, ObservableObject, CLLocationManagerDelegate {
    
    let manager = CLLocationManager()

    @Published var location: CLLocationCoordinate2D?

    override init() {
        super.init()
        manager.delegate = self
    }

    func requestLocation() {
        manager.requestLocation()
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        location = locations.first?.coordinate
    }
}

struct ContentView: View {
    @StateObject var locationManager = LocationManager()

    var body: some View {
        VStack {
            
            if let location = locationManager.location {
                Text("Your location: \(location.latitude), \(location.longitude)")
            }
            
            LocationButton {
                locationManager.requestLocation()
            }
            .frame(height: 44)
            .padding()
        }
    }
}


struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

I'm getting the following error message when trying to get GPS coordinates on iOS using (Swift, SwiftUI), based on the following tutorial:

The error message

Thread 1: "Delegate must respond to locationManager:didFailWithError:"

The URL of the tutorial

https://www.hackingwithswift.com/quick-start/swiftui/how-to-read-the-users-location-using-locationbutton

My question is:

  • Is the tutorial incomplete?
  • Do I have to add some additional code to try out this app?
  • What does the error message mean?

The code (taken from the tutorial) is:

import SwiftUI

import CoreLocation
import CoreLocationUI

class LocationManager: NSObject, ObservableObject, CLLocationManagerDelegate {
    
    let manager = CLLocationManager()

    @Published var location: CLLocationCoordinate2D?

    override init() {
        super.init()
        manager.delegate = self
    }

    func requestLocation() {
        manager.requestLocation()
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        location = locations.first?.coordinate
    }
}

struct ContentView: View {
    @StateObject var locationManager = LocationManager()

    var body: some View {
        VStack {
            
            if let location = locationManager.location {
                Text("Your location: \(location.latitude), \(location.longitude)")
            }
            
            LocationButton {
                locationManager.requestLocation()
            }
            .frame(height: 44)
            .padding()
        }
    }
}


struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

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

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

发布评论

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

评论(1

看透却不说透 2025-02-16 15:50:07

正如Kishan Bhatiya评论中建议的那样,代码的以下添加(通过提供错误处理):


import SwiftUI

import CoreLocation
import CoreLocationUI

class LocationManager: NSObject, ObservableObject, CLLocationManagerDelegate {
    
    let manager = CLLocationManager()

    @Published var location: CLLocationCoordinate2D?

    override init() {
        super.init()
        manager.delegate = self
    }

    func requestLocation() {
        manager.requestLocation()
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        location = locations.first?.coordinate
    }
    
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        // Error handling
        print("Error requesting location")
    }
}

struct ContentView: View {
    @StateObject var locationManager = LocationManager()

    var body: some View {
        VStack {
            
            if let location = locationManager.location {
                Text("Your location: \(location.latitude), \(location.longitude)")
            }
            
            LocationButton {
                locationManager.requestLocation()
            }
            .frame(height: 44)
            .padding()
        }
    }
}


struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
    

As suggested in the comments by Kishan Bhatiya, the following addition to the code fixes it (by providing error handling):


import SwiftUI

import CoreLocation
import CoreLocationUI

class LocationManager: NSObject, ObservableObject, CLLocationManagerDelegate {
    
    let manager = CLLocationManager()

    @Published var location: CLLocationCoordinate2D?

    override init() {
        super.init()
        manager.delegate = self
    }

    func requestLocation() {
        manager.requestLocation()
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        location = locations.first?.coordinate
    }
    
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        // Error handling
        print("Error requesting location")
    }
}

struct ContentView: View {
    @StateObject var locationManager = LocationManager()

    var body: some View {
        VStack {
            
            if let location = locationManager.location {
                Text("Your location: \(location.latitude), \(location.longitude)")
            }
            
            LocationButton {
                locationManager.requestLocation()
            }
            .frame(height: 44)
            .padding()
        }
    }
}


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