gpt4 book ai didi

ios - 创建全局 CLLocationManager 时出错

转载 作者:行者123 更新时间:2023-11-30 12:18:32 28 4
gpt4 key购买 nike

Apple Docs 建议不要将 CLLocationManager 存储在本地变量中。因此,我在 import 语句之后在我的 ViewController 类的范围之外创建了一个全局常量。但是,尝试访问类内部的常量会引发编译器错误:

Xcode example screenshot

类似声明的 NSMutableDictionary 类型的 globalDictionary 常量似乎可以在类内部访问。

我在这里做错了什么? 为什么上面的方法不起作用?

<小时/>

代码:

//
// ViewController.swift
// CoreLocationExample
//
//

import UIKit
import CoreLocation
import Foundation

let locationManager = CLLocationManager()
let globalDictionary = NSMutableDictionary()
class ViewController: UIViewController, CLLocationManagerDelegate {

// let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()

let dict = [
"name":"John"
]
globalDictionary.addEntries(from: dict)
print(globalDictionary)
}
}
<小时/>

在 Xcode 版本 8.3.1 上使用 Swift 3

最佳答案

Apple 文档建议不要将 CLLocationManager 存储在本地变量中。这意味着不要在方法/函数内创建本地实例。

请这样声明。

AppDelegate 类:UIResponder、UIApplicationDelegate {

//CLLocation Manager
let locationManager = CLLocationManager()
var locValue = CLLocationCoordinate2D()

}

CLLocation Manager 或 globalDictionary 在类内。

关于ios - 创建全局 CLLocationManager 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45100032/

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