创建了一个函数,但是在if语句中,该函数无法快速识别

时间:2018-12-16 00:16:45

标签: ios swift

我制作了函数getLocation(),并在有效的else{ getLocation()}中使用了它。稍后,当我尝试定义新功能locationmanager时,if...{ getLocation()}收到错误:

  

使用未解析的标识符“ getLocation”

我不确定语法是否正确或是否有另一种方法调用getLocation函数。

import UIKit
import MapKit

class ViewController: UIViewController, CLLocationManagerDelegate {
    var coreLocationManager = CLLocationManager()
    var locationManager: LocationManager!

    @IBOutlet weak var mapView: MKMapView!
    @IBOutlet weak var location_info: UILabel!
    @IBAction func Update_Location(_ sender: Any) {
    }


    override func viewDidLoad() {
        super.viewDidLoad()

        coreLocationManager.delegate=self
        locationManager = LocationManager.sharedInstance

        let authorizationCode = CLLocationManager.authorizationStatus()

        if authorizationCode == CLAuthorizationStatus.notDetermined &&
            coreLocationManager.responds(to: #selector(CLLocationManager.requestWhenInUseAuthorization))||coreLocationManager.responds(to: #selector(CLLocationManager.requestAlwaysAuthorization)){}
        if Bundle.main.object(forInfoDictionaryKey: "NSlocationWhenInUseUsageDescription") != nil{
            coreLocationManager.requestWhenInUseAuthorization()
        }else{
            getLocation()
        }
    }

    func getLocation(){
        locationManager.startUpdatingLocationWithCompletionHandler{(latitude, longitude, status, verboseMessage, error)->()in
            self.displayLocation(location: CLLocation(latitude: latitude, longitude: longitude))
        }
    }

    func displayLocation(location:CLLocation){
        mapView.setRegion(MKCoordinateRegion(center:     CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude),span: MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05)), animated:true)
        let locationPinCoord = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
        let annotation = MKPointAnnotation()
        annotation.coordinate = locationPinCoord

        mapView.addAnnotation(annotation)
        mapView.showAnnotations([annotation], animated: true)

        locationManager.reverseGeocodeLocationWithCoordinates(location) { (reverseGecodeInfo, placemark, error) in
            print(reverseGecodeInfo!)
        }
    }
}

func locationmanager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus){
    if status != CLAuthorizationStatus.notDetermined || status != CLAuthorizationStatus.denied || status != CLAuthorizationStatus.restricted {
        getLocation()
    }
}

1 个答案:

答案 0 :(得分:0)

帮自己一个忙,并使用适当的缩进。

尝试一下,全选,剪切,然后粘贴。 Xcode会自动为您缩进,然后您会立即发现问题是您在错误的位置放置了return true