I have this error: Ambiguous use of 'locationManager'
I have this error: Ambiguous use of 'locationManager'
我正在尝试在 Swift 4 中创建一个地理围栏,但我遇到了一个问题,我在调用 locationManager 时出错,我不明白为什么会出现此错误
这是错误:
Ambiguous use of locationManager
我试图通过放置 self.locationManager.startUpdatingLocation () 和其他不同的方法来更正它,但是 none 有效
在这里我声明我的变量 locationManager:
var locationManager = CLLocationManager()
if let appDelegate = UIApplication.shared.delegate as? AppDelegate{
locationManager = appDelegate.locationManager
locationManager.delegate = self
}
这是我出错的代码块:
if let locationManager = self.locationManager{
let region = self.regionToMonitor()
if(locationManager.monitoredRegions.count > 0){
self.geofencesLabel.text = "Geofences OFF"
locationManager.stopMonitoring(for: region)
mapView.removeOverlays(mapView.overlays)
}else{
self.geofencesLabel.text = "Geofences ON"
locationManager.startMonitoring(for: region)
mapView.add(MKCircle(center: region.center, radius: region.radius))
nombreU.isHidden = true
empresaU.isHidden = true
correoElectronicoU.isHidden = true
telefonoU.isHidden = true
//Generamos el registro en la base de datos local en el servidor
let nombreEvento = self.nombreE
let nombreOficial = self.nombreO
let fechaEvento = self.fechaE
let fechaFormato = self.fechaF
let lugarEvento = self.lugarE
let descripcionEvento = self.descripcionE
let imagenEvento = self.imagenE
let latitudEvento = self.latitudE
let longitudEvento = self.longitudE
let empresaEvento = self.empresaE
let rolEvento = self.rolE
//Recuperamos el contenido de las cajas de texto para validarlo
let nombreC = self.nombreU.text!
let empresaC = self.empresaU.text!
let correoC = self.correoElectronicoU.text!
let telefonoC = self.telefonoU.text!
//Validamos que no este vacio ninguna de los datos recibidos del servidor y ninguna de las cajas de texto
if(nombreC.isEmpty || empresaC.isEmpty || correoC.isEmpty || telefonoC.isEmpty){
displayMyAlertMessage(userMessage: "Todos los campos deben ser llenados")
return
}
if(nombreEvento.isEmpty || nombreOficial.isEmpty || fechaEvento.isEmpty || fechaFormato.isEmpty || lugarEvento.isEmpty || descripcionEvento.isEmpty){
displayMyAlertMessage(userMessage: "Ocurrio un error al cargar los eventos")
dismiss(animated: true, completion: nil)
return
}
//Ingresamos los valores a la base de datos local
let newEvento = NSEntityDescription.insertNewObject(forEntityName: "Eventos", into: context)
newEvento.setValue(nombreEvento, forKey: "nombreE")
newEvento.setValue(nombreOficial, forKey: "nombreO")
newEvento.setValue(fechaEvento, forKey: "fechaE")
newEvento.setValue(fechaFormato, forKey: "fechaF")
newEvento.setValue(lugarEvento, forKey: "lugarE")
newEvento.setValue(descripcionEvento, forKey: "descripcionE")
newEvento.setValue(imagenEvento, forKey: "imagenE")
newEvento.setValue(latitudEvento, forKey: "latitudE")
newEvento.setValue(longitudEvento, forKey: "longitudE")
newEvento.setValue(empresaEvento, forKey: "empresaE")
newEvento.setValue(rolEvento, forKey: "rolE")
nombreU.isHidden = true
empresaU.isHidden = true
correoElectronicoU.isHidden = true
telefonoU.isHidden = true
//Mostramos mensaje de que ya estas registrado
mensajeRegistro.text = "Gracias por registrarte a nuestro evento"
mensajeRegistro.isHidden = false
do{
try context.save()
print("El registro al evento se realizo de forma correcta")
}catch{
print(error)
}
//Ejecutamos el codigo del WebService
guard let url = URL(string: "http://www.sitioweb.mx/webservice/registro.php?nombre=\(nom)&empresa=\(empresaC)&correo=\(corC)&telefono=\(telC)") else { return }
let session = URLSession.shared
session.dataTask(with: url) { (data, response, error) in
if let response = response {
print(response)
}else{
print(error!)
}
}
}
}else{
notify(msg: "El geofence no esta disponible")
}
我的 class 有这个扩展名:
// MARK: - Location Manager Delegate
extension RegistroViewController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
mapView.showsUserLocation = status == .authorizedAlways
}
func locationManager(_ manager: CLLocationManager, monitoringDidFailFor region: CLRegion?, withError error: Error) {
print("Monitoring failed for region with identifier: \(region!.identifier)")
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("Location Manager failed with the following error: \(error)")
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("Location update")
}
}
我不明白调用时有什么问题或应该参考什么方法self.locationManager
这是我的 Appdelegate:
import UIKit
import CoreData
import CoreLocation
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
static var menu_bool = true
static var menu_boolS = true
let locationManager = CLLocationManager()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
locationManager.delegate = self as? CLLocationManagerDelegate
locationManager.requestAlwaysAuthorization()
let options: UNAuthorizationOptions = [.badge, .sound, .alert]
UNUserNotificationCenter.current().requestAuthorization(options: options){ success, error in
if let error = error{
print("Error: \(error)")
}
}
return true
}
如果locationManager
是实例属性直接使用
let locationManager = CLLocationManager()
let region = self.regionToMonitor()
if(locationManager.monitoredRegions.count > 0){
self.geofencesLabel.text = "Geofences OFF"
locationManager.stopMonitoring(for: region)
mapView.removeOverlays(mapView.overlays)
}else{
self.geofencesLabel.text = "Geofences ON"
locationManager.startMonitoring(for: region)
mapView.add(MKCircle(center: region.center, radius: region.radius))
没有
if let locationManager = self.locationManager{
我正在尝试在 Swift 4 中创建一个地理围栏,但我遇到了一个问题,我在调用 locationManager 时出错,我不明白为什么会出现此错误
这是错误:
Ambiguous use of locationManager
我试图通过放置 self.locationManager.startUpdatingLocation () 和其他不同的方法来更正它,但是 none 有效
在这里我声明我的变量 locationManager:
var locationManager = CLLocationManager()
if let appDelegate = UIApplication.shared.delegate as? AppDelegate{
locationManager = appDelegate.locationManager
locationManager.delegate = self
}
这是我出错的代码块:
if let locationManager = self.locationManager{
let region = self.regionToMonitor()
if(locationManager.monitoredRegions.count > 0){
self.geofencesLabel.text = "Geofences OFF"
locationManager.stopMonitoring(for: region)
mapView.removeOverlays(mapView.overlays)
}else{
self.geofencesLabel.text = "Geofences ON"
locationManager.startMonitoring(for: region)
mapView.add(MKCircle(center: region.center, radius: region.radius))
nombreU.isHidden = true
empresaU.isHidden = true
correoElectronicoU.isHidden = true
telefonoU.isHidden = true
//Generamos el registro en la base de datos local en el servidor
let nombreEvento = self.nombreE
let nombreOficial = self.nombreO
let fechaEvento = self.fechaE
let fechaFormato = self.fechaF
let lugarEvento = self.lugarE
let descripcionEvento = self.descripcionE
let imagenEvento = self.imagenE
let latitudEvento = self.latitudE
let longitudEvento = self.longitudE
let empresaEvento = self.empresaE
let rolEvento = self.rolE
//Recuperamos el contenido de las cajas de texto para validarlo
let nombreC = self.nombreU.text!
let empresaC = self.empresaU.text!
let correoC = self.correoElectronicoU.text!
let telefonoC = self.telefonoU.text!
//Validamos que no este vacio ninguna de los datos recibidos del servidor y ninguna de las cajas de texto
if(nombreC.isEmpty || empresaC.isEmpty || correoC.isEmpty || telefonoC.isEmpty){
displayMyAlertMessage(userMessage: "Todos los campos deben ser llenados")
return
}
if(nombreEvento.isEmpty || nombreOficial.isEmpty || fechaEvento.isEmpty || fechaFormato.isEmpty || lugarEvento.isEmpty || descripcionEvento.isEmpty){
displayMyAlertMessage(userMessage: "Ocurrio un error al cargar los eventos")
dismiss(animated: true, completion: nil)
return
}
//Ingresamos los valores a la base de datos local
let newEvento = NSEntityDescription.insertNewObject(forEntityName: "Eventos", into: context)
newEvento.setValue(nombreEvento, forKey: "nombreE")
newEvento.setValue(nombreOficial, forKey: "nombreO")
newEvento.setValue(fechaEvento, forKey: "fechaE")
newEvento.setValue(fechaFormato, forKey: "fechaF")
newEvento.setValue(lugarEvento, forKey: "lugarE")
newEvento.setValue(descripcionEvento, forKey: "descripcionE")
newEvento.setValue(imagenEvento, forKey: "imagenE")
newEvento.setValue(latitudEvento, forKey: "latitudE")
newEvento.setValue(longitudEvento, forKey: "longitudE")
newEvento.setValue(empresaEvento, forKey: "empresaE")
newEvento.setValue(rolEvento, forKey: "rolE")
nombreU.isHidden = true
empresaU.isHidden = true
correoElectronicoU.isHidden = true
telefonoU.isHidden = true
//Mostramos mensaje de que ya estas registrado
mensajeRegistro.text = "Gracias por registrarte a nuestro evento"
mensajeRegistro.isHidden = false
do{
try context.save()
print("El registro al evento se realizo de forma correcta")
}catch{
print(error)
}
//Ejecutamos el codigo del WebService
guard let url = URL(string: "http://www.sitioweb.mx/webservice/registro.php?nombre=\(nom)&empresa=\(empresaC)&correo=\(corC)&telefono=\(telC)") else { return }
let session = URLSession.shared
session.dataTask(with: url) { (data, response, error) in
if let response = response {
print(response)
}else{
print(error!)
}
}
}
}else{
notify(msg: "El geofence no esta disponible")
}
我的 class 有这个扩展名:
// MARK: - Location Manager Delegate
extension RegistroViewController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
mapView.showsUserLocation = status == .authorizedAlways
}
func locationManager(_ manager: CLLocationManager, monitoringDidFailFor region: CLRegion?, withError error: Error) {
print("Monitoring failed for region with identifier: \(region!.identifier)")
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("Location Manager failed with the following error: \(error)")
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("Location update")
}
}
我不明白调用时有什么问题或应该参考什么方法self.locationManager
这是我的 Appdelegate:
import UIKit
import CoreData
import CoreLocation
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
static var menu_bool = true
static var menu_boolS = true
let locationManager = CLLocationManager()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
locationManager.delegate = self as? CLLocationManagerDelegate
locationManager.requestAlwaysAuthorization()
let options: UNAuthorizationOptions = [.badge, .sound, .alert]
UNUserNotificationCenter.current().requestAuthorization(options: options){ success, error in
if let error = error{
print("Error: \(error)")
}
}
return true
}
如果locationManager
是实例属性直接使用
let locationManager = CLLocationManager()
let region = self.regionToMonitor()
if(locationManager.monitoredRegions.count > 0){
self.geofencesLabel.text = "Geofences OFF"
locationManager.stopMonitoring(for: region)
mapView.removeOverlays(mapView.overlays)
}else{
self.geofencesLabel.text = "Geofences ON"
locationManager.startMonitoring(for: region)
mapView.add(MKCircle(center: region.center, radius: region.radius))
没有
if let locationManager = self.locationManager{