swift - 运行模拟器时没有打印坐标,但是当我在我的 iPhone 上运行程序时
问题描述
我有以下代码
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, CLLocationManagerDelegate {
var locationManager = CLLocationManager()
var coordinates: String?
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.requestAlwaysAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
//updates the user's location as the user moves
let location: CLLocation = locations[0] as CLLocation
coordinates = "\(location.coordinate.latitude),\(location.coordinate.longitude)"
print(coordinates!)
}
当我在模拟器上运行它时,程序运行良好,但它不打印坐标。当我在 iPhone 上运行它时,它会打印坐标。我需要更改 Xcode 设置中的某些内容以解决此问题吗?此外,当我尝试在另一个函数中使用“坐标”变量时,我得到一个错误,说编译器正在找到 nil,即使我在 locationManager 函数中为其分配了一个值,所以我猜测这两个问题是相关的。提前致谢
解决方案
您可以在运行应用程序时使用“模拟位置”按钮在模拟器上模拟位置:
您可以选择现有位置之一,也可以创建 GPX 文件(例如,不是静态单点,而是模拟位置不断变化的路线)。我建议您使用https://mapstogpx.com/创建 GPX 文件。
推荐阅读
- python - 在 Python 2.7 中使用翻译
- javascript - Laravel刀片文件中Javascript中的范围问题
- sql - 从多个表查询中计算特定表行数
- sql-server - 大型 Fact 表中 NCI dateKey 上的 sql update 语句的最佳实践
- android - Delphi 不会为 android 项目创建 APK
- amazon-web-services - AWS Cognito 托管 UI 和防止并发用户会话
- node.js - SAM Node.js hello world 应用缺少 axios 模块
- sql - 重建所有表中的所有索引但需要排除某些表
- wordpress - 收藏图标仅显示在管理端,不反映在前端
- gitlab - Gitlab CI - 在 before_script 中指定阶段