xcode 当应用处理委托时,抛出“委托必须响应locationManager:didUpdateLocations:“错误

k4emjkb1  于 2022-11-17  发布在  其他
关注(0)|答案(1)|浏览(260)

现在我只是想把位置打印在屏幕上,稍后会详细说明,但我得到了以下控制台输出(应用程序也崩溃-它正在物理设备上构建和运行):

2022-11-14 16:43:07.333908-0700 Countries[7257:1687502] *** Assertion failure in -[CLLocationManager requestLocation], CLLocationManager.m:1323
2022-11-14 16:43:07.334806-0700 Countries[7257:1687502] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Delegate must respond to locationManager:didUpdateLocations:'
*** First throw call stack:
(0x1c4566248 0x1bd92ba68 0x1beeea81c 0x1cfd2a654 0x1040d7308 0x1040d3ff8 0x246a377e8 0x1c69396d8 0x1c69393d8 0x1c6938d6c 0x2464f75f8 0x1c6938848 0x1c681e060 0x1c709e1d8 0x1c67e1d18 0x1c67e6674 0x1c67e5944 0x1c67e5000 0x1c682cd64 0x1c7473a30 0x1c6d3b678 0x1c737a904 0x1c7379ad0 0x1c463222c 0x1c463e614 0x1c45c251c 0x1c45d7eb8 0x1c45dd1e4 0x1fd3fd368 0x1c6a8cd88 0x1c6a8c9ec 0x1c808bce8 0x1c7fe8c24 0x1c7fd1b44 0x1040e8b20 0x1040e8bc8 0x1e2901948)
libc++abi: terminating with uncaught exception of type NSException
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Delegate must respond to locationManager:didUpdateLocations:'
terminating with uncaught exception of type NSException
(lldb)

下面是我使用的代码:

import Foundation

import CoreLocation
import CoreLocationUI

class LocationManager: NSObject, ObservableObject, CLLocationManagerDelegate {
    
    let manager = CLLocationManager()
    
    @Published var location: CLLocationCoordinate2D?
    
    override init() {
        super.init()
        manager.delegate = self
        manager.requestWhenInUseAuthorization()
    }
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation], didFailWithError error: Error) {
        location = locations.first?.coordinate
    }
    
    func requestLocation() {
        manager.requestLocation()
    }
}

视图的代码(在不同的Swift文件中):

import SwiftUI
import _CoreLocationUI_SwiftUI

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

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

struct SettingsView_Previews: PreviewProvider {
    static var previews: some View {
        SettingsView()
    }
}

我试过在物理设备和模拟器上重新启动项目并运行构建。当按下LocationButton时,两者都导致相同的崩溃和输出。

guz6ccqo

guz6ccqo1#

错误是正确的。您尚未实现locationManager:didUpdateLocations:方法。您有一个名为locationManager:didUpdateLocations:didFailWithError:的方法。
而不是:

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

您需要(删除didFailWithError参数):

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

如果您想要行程任何错误,请同时实作locationManager(_:didFailWithError:)委派方法。

相关问题