gpt4 book ai didi

swift - Swift Playground 中的 CoreLocation

转载 作者:行者123 更新时间:2023-11-28 06:22:01 25 4
gpt4 key购买 nike

我正在尝试检索用户的当前位置,但在 Swift Playgrounds 中。我已经有了这段代码,但由于某种原因它从不执行打印语句。谁能帮忙?

import UIKit  
import CoreLocation
import PlaygroundSupport

var str = "Hello, playground"
let locationManager = CLLocationManager()
PlaygroundPage.current.needsIndefiniteExecution = true

class GeoCoordinateDelegate: NSObject, CLLocationManagerDelegate {

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[0]
print("Most Recent Location: " + location.description)
print(location.coordinate.latitude)
print(location.coordinate.longitude)
}

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("Error while updating " + error.localizedDescription)
}

}
let geoCoordinateDelegate = GeoCoordinateDelegate()
locationManager.delegate = geoCoordinateDelegate
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()

最佳答案

编写 CoreLocation 代码时,最好在尝试执行任何与位置相关的操作之前检查 CLLocationManager.locationServicesEnabled()

在 Swift Playgrounds(2017 年 3 月 21 日的 1.2)中,该方法返回 false,因为 CoreLocation 不适用于 playgrounds(还?)。我建议在 https://bugreport.apple.com/ 提交错误报告

关于swift - Swift Playground 中的 CoreLocation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43125492/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com