当我运行此代码时,“ switch”和“ extension”行会出错。为什么?

时间:2019-10-04 13:13:49

标签: ios swift

import UIKit
import MapKit
import CoreLocation

class MapViewController: UIViewController {

    @IBOutlet weak var mapView: MKMapView!

    let locationManager = CLLocationManager()
    let regionInMeters: Double = 2000


    @IBAction func changeMapType(_ sender: UISegmentedControl) {
        if sender.selectedSegmentIndex == 0 {
            mapView.mapType = .standard
        } else {
            mapView.mapType = .satellite
        }

    }

    override func viewDidLoad() {
        super.viewDidLoad()

        locationManager.requestWhenInUseAuthorization()
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.distanceFilter = kCLDistanceFilterNone
        locationManager.startUpdatingLocation()


        func checkLocationAuthorization() {
            switch CLLocationManager.authorizationStatus() {
            case .authorizedWhenInUse:
                mapView.showsUserLocation = true
                centerViewOnUserLocation()

            }
        }


        func centerViewOnUserLocation() {
            if let location = locationManager.location?.coordinate {
                let region = MKCoordinateRegion.init(center: location, latitudinalMeters: regionInMeters, longitudinalMeters: regionInMeters)
                mapView.setRegion(region, animated: true)
            }

        }

    extension MapScreen: CLLocationManagerDelegate {

        func locationManager( manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        }

        func locationManager( manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        }
    }
    }

它说

  

“开关必须详尽无遗”

在功能checkLocationAuthorization上显示

  

“声明仅在文件范围内有效”

在扩展名上?我该如何解决?

1 个答案:

答案 0 :(得分:0)

在checkLocationAuthorization中,处理所有authorizationStatus的情况或添加如下所示的默认情况

class A {

}

extension A: <some protocol> {

}

// MARK: Doing a spefic work!! 
extension A { 

}

扩展

扩展名应在控制器范围之外。

{{1}}

swift extension